Java-Jackson反序列化-如何反序列化接口数组?
我有这样的数组Java-Jackson反序列化-如何反序列化接口数组?,java,arrays,json,jackson,deserialization,Java,Arrays,Json,Jackson,Deserialization,我有这样的数组 { ImplArray1:[ { ... } , ... ], ImplArray2:[ { ... } , ... ] } Impl1和Impl2都实现相同的接口 如何将两个数组反序列化为两个列表 编辑:我的上帝。这个问题毫无意义。真的很抱歉我的生活很糟糕 我的意思是,数组implary1包含Impl1
{
ImplArray1:[
{
...
}
, ...
],
ImplArray2:[
{
...
}
, ...
]
}
Impl1和Impl2都实现相同的接口
如何将两个数组反序列化为两个列表我的意思是,数组implary1包含Impl1,数组implary2包含Impl2。Impl1和Impl2都实现相同的接口。如何将两者反序列化到List
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
{
}
ObjectMapper om=new ObjectMapper();
TypeFactory tf=om.getTypeFactory();
//映射键类型
JavaType stringType=tf.constructType(String.class);
//映射值类型
JavaType listOfSuper=tf.ConstructParameterizedType(List.class、List.class、Super.class);
//地图类型
JavaType mapType=tf.constructMapType(Map.class、stringType、listOfSuper);
//最后,进行语法分析
Reader fr=new FileReader(“C://Temp/xx.json”);
Map Map=(Map)om.readValue(fr,mapType);
静态接口
{
}
静态类超级实现接口
{
公共字符串名称=”;
公共年龄=0;
}
静态类Impl1扩展了Super
{
}
静态类Impl2扩展了Super
{
}
您的意思是Impl1和Impl2都实现相同的接口?也许这个答案有帮助,序列化/反序列化