本文介绍了杰克逊解析json中的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例json数据

I have sample json data like below

试图解析杰克逊解析器中的数据字段,并为其值创建ObjectCategories类(setter getter).

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");
}

如果执行上面的代码,则出现此异常- getCategories().get(0).getAccuracy() java.util.LinkedHashMap不能转换为ObjectCategory

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)返回Map值.如何解析我的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>>() {});

这篇关于杰克逊解析json中的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 06:00