我正在使用Jackson-jr来读取我的JSON文件。其中一个有点复杂:
[
{"test-1":["a","b","c","d"]},
{"test-2":["b","j","d"]},
{"test-3":["n","e","o","p","i"]},
{"test-4":["s","a","v","z","b","ç","x","p"]},
{"test-5":["d","q","u"]},
{"test-6":["f","b"]}
]
我要读的代码非常简单:
InputStream inputStream = assetManager.open("test.json");
ArrayList<HashMap<String, ArrayList<String>>> arrays = JSON.std.beanFrom(ArrayList.class, inputStream);
我的目的是让每个数组都能与它们一起工作:
arrays.get(i);
但是,当执行这一行时,我有一个异常:
Caused by: java.lang.ClassCastException: com.fasterxml.jackson.jr.ob.impl.DeferredMap cannot be cast to java.util.HashMap
杰克逊-jr没有TypeFactory、TypeReference或ObjectMapper。一些我为jackson找到答案的链接,而不是jackson-jr:
ClassCastException when convert json to list of objects
Jackson custom deserializer for one field with polymorphic types
Tricky(?) JSON, polymorphic deserialization
在Jackson-Jr有没有办法处理这件事?
发布于 2016-09-21 05:58:26
您应该对接口编程,而不是对实现类进行编程。由于返回的映射实现是DeferredMap
而不是HashMap
,因此您将获得ClassCastException
。更改分配,如下所示
List<Map<String, List<String>>> arrays = JSON.std.beanFrom(ArrayList.class, inputStream);
https://stackoverflow.com/questions/39603834
复制相似问题