简体   繁体   中英

Jackson Parsing for json object inside json

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM