json 内的 json 对象的 Jackson 解析
Jackson Parsing for json object inside json
我有如下示例 json 数据
{"data":{"detection":[{"category":"building","coordinates":{"xmin":"0.31","ymin":"0.42","ymax":"0.82","xmax":"0.89"},"accuracy":"0.66"}]}}
正在尝试解析 jackson 解析器中的数据字段并为其值创建了 ObjectCategories class(setter getter)。
@JsonProperty("categories")
private List<ObjectCategory> categories;
@SuppressWarnings("unchecked")
@JsonProperty(DATA)
private void unpackNested(Map<String,Object> data) {
this.categories = (ArrayList<ObjectCategory>) data.get("detection");
}
如果我们执行上面的代码,得到这个异常 - getCategories().get(0).getAccuracy() to java.util.LinkedHashMap 不能被转换为 ObjectCategory
getCategories().get(0) returns 地图值。如何使用我的 ObjectCategory class.
进行解析
如果您最初将值反序列化为映射,则可以转换该值。
this.categories = objectMapper
.convertValue(data.get("detection"),
new TypeReference<List<ObjectCategory>>() {});
我有如下示例 json 数据
{"data":{"detection":[{"category":"building","coordinates":{"xmin":"0.31","ymin":"0.42","ymax":"0.82","xmax":"0.89"},"accuracy":"0.66"}]}}
正在尝试解析 jackson 解析器中的数据字段并为其值创建了 ObjectCategories class(setter getter)。
@JsonProperty("categories")
private List<ObjectCategory> categories;
@SuppressWarnings("unchecked")
@JsonProperty(DATA)
private void unpackNested(Map<String,Object> data) {
this.categories = (ArrayList<ObjectCategory>) data.get("detection");
}
如果我们执行上面的代码,得到这个异常 - getCategories().get(0).getAccuracy() to java.util.LinkedHashMap 不能被转换为 ObjectCategory
getCategories().get(0) returns 地图值。如何使用我的 ObjectCategory class.
进行解析如果您最初将值反序列化为映射,则可以转换该值。
this.categories = objectMapper
.convertValue(data.get("detection"),
new TypeReference<List<ObjectCategory>>() {});