Jackson Parsing for json object inside json

守給你的承諾、 提交于 2020-01-24 00:48:09

问题


I have sample json data like below

{"data":{"detection":[{"category":"building","coordinates":{"xmin":"0.31","ymin":"0.42","ymax":"0.82","xmax":"0.89"},"accuracy":"0.66"}]}}

Trying to parse data field in jackson parser and created ObjectCategories class(setter getter) for its values.

@JsonProperty("categories")
private List<ObjectCategory> categories;

@SuppressWarnings("unchecked")
@JsonProperty(DATA)
private void unpackNested(Map<String,Object> data) {
    this.categories = (ArrayList<ObjectCategory>) data.get("detection");
}

If we execute the above code, getting this exception - getCategories().get(0).getAccuracy() to java.util.LinkedHashMap cannot be cast to ObjectCategory

getCategories().get(0) returns Map value. How to parse with my ObjectCategory class.


回答1:


You can convert the value if you originally deserialized it to map.

this.categories = objectMapper
     .convertValue(data.get("detection"), 
                   new TypeReference<List<ObjectCategory>>() {});


来源:https://stackoverflow.com/questions/50503471/jackson-parsing-for-json-object-inside-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!