问题
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