[英]Deserialization using Jackson Json where one of the keys is dynamic
I am fairly new to Jackson. 我对杰克逊很陌生。 I am trying to map the following json to a POJO using Jackson for deserialization.
我正在尝试将以下json映射到使用Jackson进行反序列化的POJO。
{
"data": [
{
"customerName": "abc",
"varaible_Key1": {
"p1": "text data",
"p2": "textarea data",
........
}
},
{
"customerName": "bbc",
"varaible_Key2": {
"p1": "text",
"p2": "textarea"
......
}
},
{
"customerName": "xyz",
"varaible_Key3": {
"p1": "xyz text",
"p2": "xyz textarea"
......
}
}
///////more customername / variable_keys
]
}
The problem I am facing is with dynamic / variable keys in the json. 我面临的问题是json中的动态/可变键。
I have tried using @JsonAnySetter in the POJO as shown below. 我尝试在POJO中使用@JsonAnySetter,如下所示。
public class Foo {
@JsonProperty("customerName")
private String name;
private Map<String, DataObject> properties;
@JsonAnyGetter
public Map<String, DataObject> getProperties() {
return properties;
}
@JsonAnySetter
public void add(String key, DataObject value) {
properties.put(key, value);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
where DataObject contains the fields like p1,p2 and so on. 其中DataObject包含p1,p2等字段。
public class DataObject {
@JsonProperty("p1")
private String firstValue;
@JsonProperty("p2")
private String secondValue;
@JsonProperty("p3")
private String thirdValue;
@JsonProperty("p4")
private String fourthValue;
public String getFirstValue() {
return firstValue;
}
public void setFirstValue(String firstValue) {
this.firstValue = firstValue;
}
public String getSecondValue() {
return secondValue;
}
public void setSecondValue(String secondValue) {
this.secondValue = secondValue;
}
public String getThirdValue() {
return thirdValue;
}
public void setThirdValue(String thirdValue) {
this.thirdValue = thirdValue;
}
public String getFourthValue() {
return fourthValue;
}
public void setFourthValue(String fourthValue) {
this.fourthValue = fourthValue;
}
}
I keep getting the below error. 我不断收到以下错误。 Any help on this is appreciated.
在这方面的任何帮助表示赞赏。
com.fasterxml.jackson.databind.JsonMappingException: N/A (through reference chain: com.epic.customer.dto.DataField["data"]->java.util.ArrayList[0]-> com.epic.customer.dto.Foo["varaible_Key1"]) at com.fasterxml.jackson.databind.deser.SettableAnyProperty._throwAsIOE(SettableAnyProperty.java:214) at com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:179) at com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:134) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1539) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:285) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize
com.fasterxml.jackson.databind.JsonMappingException:不适用(通过参考链:com.epic.customer.dto.DataField [“ data”]-> java.util.ArrayList [0]-> com.epic.customer。 com.fasterxml.jackson.databind.deser.SettableAnyProperty._throwAsIOE(SettableAnyProperty.java:214)上的dto.Foo [“ varaible_Key1”]),com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.set(179) )的com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1539)的com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:134)处。位于com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)处的databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)位于com.fasterxml.jackson.databind.deser.std.CollectionDeserializer处。在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize中进行deserialize(CollectionDeserializer.java:285) (CollectionDeserializer.java:244) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
(CollectionDeserializer.java:244)在com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)在com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java :127),位于com.fasterxml.jack.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287),位于com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)的com.fasterxml。 jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
You just need to create 2 setters, for specific property: 您只需要为特定属性创建2个setter:
class Foo {
private DataObject dataObject;
public DataObject getDataObject() {
return dataObject;
}
public void setVaraible_Key1(DataObject dataObject) {
this.dataObject = dataObject;
}
public void setVaraible_Key2(DataObject dataObject) {
this.dataObject = dataObject;
}
}
I think you have to give it a hint on the type of object you want back: 我认为您必须对要返回的对象类型提供提示:
ObjectMapper mapper = new ObjectMapper();
Map<String, DataObject> test = mapper.readValue("Insert Data in here", mapper.getTypeFactory().constructMapLikeType(HashMap.class, String.class, DataObject.class));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.