将 JSON 反序列化为 ArrayList<POJO>使用杰克逊 [英] Deserialize JSON to ArrayList<POJO> using Jackson
问题描述
我有一个 Java 类 MyPojo
,我对从 JSON 反序列化感兴趣.我配置了一个特殊的 MixIn 类 MyPojoDeMixIn
,以帮助我进行反序列化.MyPojo
只有 int
和 String
实例变量结合了适当的 getter 和 setter.MyPojoDeMixIn
看起来像这样:
I have a Java class MyPojo
that I am interested in deserializing from JSON. I have configured a special MixIn class, MyPojoDeMixIn
, to assist me with the deserialization. MyPojo
has only int
and String
instance variables combined with proper getters and setters. MyPojoDeMixIn
looks something like this:
public abstract class MyPojoDeMixIn {
MyPojoDeMixIn(
@JsonProperty("JsonName1") int prop1,
@JsonProperty("JsonName2") int prop2,
@JsonProperty("JsonName3") String prop3) {}
}
在我的测试客户端中,我执行以下操作,但当然它在编译时不起作用,因为存在与类型不匹配相关的 JsonMappingException
.
In my test client I do the following, but of course it does not work at compile time because there is a JsonMappingException
related to a type mismatch.
ObjectMapper m = new ObjectMapper();
m.getDeserializationConfig().addMixInAnnotations(MyPojo.class,MyPojoDeMixIn.class);
try { ArrayList<MyPojo> arrayOfPojo = m.readValue(response, MyPojo.class); }
catch (Exception e) { System.out.println(e) }
我知道我可以通过创建一个只有一个 ArrayList<MyPojo>
的Response"对象来缓解这个问题,但是我必须为每个对象创建这些有点无用的对象我想返回的单一类型.
I am aware that I could alleviate this issue by creating a "Response" object that has only an ArrayList<MyPojo>
in it, but then I would have to create these somewhat useless objects for every single type I want to return.
我还在线查看了JacksonInFiveMinutes,但在理解Map
以及它与我的问题的关系.如果您不知道,我对 Java 完全陌生并且来自 Obj-C 背景.他们特别提到:
I also looked online at JacksonInFiveMinutes but had a terrible time understanding the stuff about Map<A,B>
and how it relates to my issue. If you cannot tell, I'm entirely new to Java and come from an Obj-C background. They specifically mention:
除了绑定到 POJO 和简单"类型之外,还有一个附加变体:绑定到通用(类型化)容器的变体.由于所谓的类型擦除,这种情况需要特殊处理(被 Java 用来以某种向后兼容的方式实现泛型方式),这会阻止您使用类似Collection.class(不编译).
In addition to binding to POJOs and "simple" types, there is one additional variant: that of binding to generic (typed) containers. This case requires special handling due to so-called Type Erasure (used by Java to implement generics in somewhat backwards compatible way), which prevents you from using something like Collection.class (which does not compile).
因此,如果要将数据绑定到 Map 中,则需要使用:
So if you want to bind data into a Map you will need to use:
Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });
如何直接反序列化为ArrayList
?
推荐答案
您可以使用 TypeReference
包装器直接反序列化为列表.示例方法:
You can deserialize directly to a list by using the TypeReference
wrapper. An example method:
public static <T> T fromJSON(final TypeReference<T> type,
final String jsonPacket) {
T data = null;
try {
data = new ObjectMapper().readValue(jsonPacket, type);
} catch (Exception e) {
// Handle the problem
}
return data;
}
并且是这样使用的:
final String json = "";
Set<POJO> properties = fromJSON(new TypeReference<Set<POJO>>() {}, json);
这篇关于将 JSON 反序列化为 ArrayList<POJO>使用杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!