【发布时间】:2015-01-30 14:57:56
【问题描述】:
{
"query":{
"data":{
"val1":{
"id":123,
"name":"abc"
},
"val2":{
"id":13,
"name":"def"
},
"total":{
"count":2,
"page":{
"num":1
}
}
}
}
}
我的 json 看起来像上面那样。 "val1",val2" 是动态的,所以我将它映射到 Map.Everything 工作正常,直到我得到 "total" 标签。 由于结构不同,到对象的映射失败。 如何在解析或解析“total”到不同对象时跳过“total”标签。
我得到以下异常
Exception in thread "main" java.lang.IllegalArgumentException: Instantiation of [simple type, class jsom.schema.pojo.Definitions] value failed: Unrecognized field "count" (Class jsom.schema.pojo.MainResourceObj), not marked as ignorable
at [Source: java.io.StringReader@5933cca2; line: 4, column: 26] (through reference chain: jsom.schema.pojo.Definitions["definitions"]->jsom.schema.pojo.MainResourceObj["count"])
我像下面这样更新了我的 pojo,但总标签仍然进入 Map.which 导致异常
HashMap<String, customObject> definitions;
@JsonProperty("total")
Total total;
public class Total {
public Total() {
// TODO Auto-generated constructor stub
}
String count;
Page page;
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
@JsonCreator
public static Total Create(String jsonString) throws JsonParseException,
JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Total module = null;
module = mapper.readValue(jsonString, Total.class);
return module;
}
}
【问题讨论】: