简体   繁体   English

使用Jackson将Json数组映射到POJO

[英]Mapping Json Array to POJO using Jackson

I have a JSON array of the form: 我有一个形式的JSON数组:

[
 [
  1232324343,
  "A",
  "B",
  3333,
  "E"
 ],
 [
  12345424343,
  "N",
  "M",
  3133,
  "R"
 ]
]

I want to map each element of the parent array to a POJO using the Jackson library. 我想使用Jackson库将父数组的每个元素映射到POJO。 I tried this: 我试过这个:

ABC abc = new ABC();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(data).get("results");

if (jsonNode.isArray()) {
    for (JsonNode node : jsonNode) {
        String nodeContent = mapper.writeValueAsString(node);
        abc = mapper.readValue(nodeContent,ABC.class);

        System.out.println("Data: " + abc.getA());
    }
}

where ABC is my POJO class and abc is the object but I get the following exception: 其中ABC是我的POJO类,abc是对象,但我得到以下异常:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.demo.json.model.ABC com.fasterxml.jackson.databind.JsonMappingException:无法反序列化com.demo.json.model.ABC的实例

EDIT: My POJO looks like this: 编辑:我的POJO看起来像这样:

class ABC{
    long time;
    String a;
    String b;
    int status;
    String c;
}

Can someone suggest a solution for this? 有人可以为此提出解决方案吗?

EDIT 2: After consulting a lot of answers on StackOverflow and other forums, I came across one solution. 编辑2:在StackOverflow和其他论坛上查阅了很多答案之后,我遇到了一个解决方案。 I mapped the returned value of readValue() method into an array of POJO objects. 我将readValue()方法的返回值映射到POJO对象数组中。

ABC[] abc = mapper.readValue(nodeContent, ABC[].class);

But now I am getting a separate exception 但现在我得到一个单独的例外

Can not construct instance of ABC: no long/Long-argument constructor/factory method to deserialize from Number value (1552572583232) 无法构造ABC的实例:没有long / Long参数构造函数/工厂方法从Number值反序列化(1552572583232)

I have tried the following but nothing worked: 1. Forcing Jackson to use ints for long values using 我尝试了以下但没有任何效果:1。强制杰克逊使用int来获取长值

mapper.configure(DeserializationFeature.USE_LONG_FOR_INTS, true);

2. Using wrapper class Long instead of long in the POJO 2.在POJO中使用包装类Long而不是long

Can anyone help me with this? 谁能帮我这个?

You can use ARRAY shape for this object. 您可以为此对象使用ARRAY形状。 You can do that using JsonFormat annotation: 你可以使用JsonFormat注释来做到这JsonFormat

@JsonFormat(shape = Shape.ARRAY)
class ABC {

And deserialise it: 并对其进行反序列化:

ABC[] abcs = mapper.readValue(json, ABC[].class);

EDIT after changes in question. 有问题的更改后编辑。

You example code could look like this: 您的示例代码可能如下所示:

JsonNode jsonNode = mapper.readTree(json);
if (jsonNode.isArray()) {
    for (JsonNode node : jsonNode) {
        String nodeContent = mapper.writeValueAsString(node);
        ABC abc = mapper.readValue(nodeContent, ABC.class);

        System.out.println("Data: " + abc.getA());
    }
}

We can use convertValue method and skip serializing process: 我们可以使用convertValue方法并跳过序列化过程:

JsonNode jsonNode = mapper.readTree(json);
if (jsonNode.isArray()) {
    for (JsonNode node : jsonNode) {
        ABC abc = mapper.convertValue(node, ABC.class);
        System.out.println("Data: " + abc.getA());
    }
}

Or even: 甚至:

JsonNode jsonNode = mapper.readTree(json);
ABC[] abc = mapper.convertValue(jsonNode, ABC[].class);
System.out.println(Arrays.toString(abc));

Your json does not map to the pojo that you have defined. 你的json没有映射到你定义的pojo。 For the pojo that you have defined, the json should be of the form below. 对于你定义的pojo,json应该是下面的形式。

{ 
  "time:1232324343,
  "a":"A",
  "b":"B",
  "status":3333,
  "c":"E"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM