我有以下JSON,我想解析下面的bean类。
{
"timeStamp": "123123123123",
"pznFlowFlag": "true",
"pznRequestFlag": "true",
"sessionId": "SampleSessionId",
"ipAddress": "172.148.0.1",
"offers": [
{
"111": {
"eep" : "44279",
"spl_ind" : "true"},
"121": {
"eep" : "44520",
"spl_ind" : "false"},
"333": {
"eep" : "45419",
"spl_ind" : "false" }
}]
}
Bean class 1
public class DistributedCookieBean {
@JsonProperty("timeStamp")
private String timeStamp;
@JsonProperty("pznFlowFlag")
private String pznFlowFlag;
@JsonProperty("pznRequestFlag")
private String pznRequestFlag;
@JsonProperty("sessionId")
private String sessionId;
@JsonProperty("ipAddress")
private String ipAddress;
@JsonProperty("offers")
private Map<String, OfferCookieBean> offers = new HashMap<String, OfferCookieBean>();
...<setters & getters>
}
Bean class 2
public class OfferCookieBean {
@JsonProperty("eep")
private String eep;
@JsonProperty("spl_ind")
private String spl_ind;
...<setters & getters>
}
这里我无法使用下面的代码解析这些Java POJO。
jsonObjMapper.readValue(jsonString,DistributedCookieBean.class);
堆栈跟踪:
org.codehaus.jackson.map.JsonMappingException:无法从START_ARRAY标记中反序列化java.util.Map的实例 在[来源:json.txt; line:6,column:31] at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:159) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:192) at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:134) at org.codehaus.jackson.map.deser.MapDeserializer.deserialize(MapDeserializer.java:23) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:135) at org.codehaus.jackson.map.deser.SettableBeanProperty $ MethodProperty.deserializeAndSet(SettableBeanProperty.java:221) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:391) 在org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:287) 在org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588) 在org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1074) 在Test.main(Test.java:29)
提前致谢!
答案 0 :(得分:2)
错误消息指出问题:offers
字段被声明为地图,而在JSON中,它是一个地图数组。
尝试将Map<String, OfferCookieBean> offers = new HashMap<String, OfferCookieBean>()
更改为List<Map<String, OfferCookieBean>> offers;
,看看会发生什么。