将JSON反序列化为ArrayList< POJO>用杰克逊 [英] Deserialize JSON to ArrayList<POJO> using Jackson

查看:99
本文介绍了将JSON反序列化为ArrayList< POJO>用杰克逊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类 MyPojo ,我有兴趣从JSON反序列化。我已经配置了一个特殊的MixIn类, MyPojoDeMixIn ,以帮助我进行反序列化。 MyPojo 只有 int 字符串实例变量与正确相结合吸气剂和二传手。 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) }

I我知道我可以通过创建一个只有 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< A,B> 以及它与我的问题的关系。如果你不能说,我是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用来实现有点向后兼容的
方式的泛型),这会阻止你使用像
这样的东西.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

How can I deserialize directly to 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);

TypeReference Javadoc

这篇关于将JSON反序列化为ArrayList&lt; POJO&gt;用杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆