我有这样的数组。
{
ImplArray1:[
{
...
}
, ...
],
ImplArray2:[
{
...
}
, ...
]
}
Impl1 和 Impl2 都实现相同的接口(interface)。
如何将两个数组反序列化为两个List
编辑:天啊。这个问题根本没有意义。真的很抱歉生活不好。
我的意思是,数组ImplArray1包含Impl1,数组ImplArray2包含Impl2。 Impl1 和 Impl2 都实现相同的接口(interface)。如何将两者反序列化为 List
最佳答案
我设法提取接近要求的东西的唯一方法是,如果您有一个由两种实现都扩展的父类(super class)。父类(super class)可以实现公共(public)接口(interface)
ObjectMapper om = new ObjectMapper();
TypeFactory tf = om.getTypeFactory();
// map key type
JavaType stringType = tf.constructType(String.class);
// map value type
JavaType listOfSuper = tf.constructParametrizedType(List.class, List.class, Super.class);
// map type
JavaType mapType = tf.constructMapType(Map.class, stringType, listOfSuper);
// finally, do the parsing
Reader fr = new FileReader("C://Temp/xx.json");
Map<String, List<Interface>> map = (Map<String, List<Interface>>)om.readValue(fr, mapType);
static interface Interface
{
}
static class Super implements Interface
{
public String name = "";
public int age = 0;
}
static class Impl1 extends Super
{
}
static class Impl2 extends Super
{
}
关于Java - Jackson反序列化 - 如何反序列化接口(interface)数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265286/