使用Jackson将Json Array映射到POJO [英] Mapping Json Array to POJO using Jackson

查看:64
本文介绍了使用Jackson将Json Array映射到POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式的JSON数组:

I have a JSON array of the form:

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

我想使用Jackson库将父数组的每个元素映射到POJO.我试过了:

I want to map each element of the parent array to a POJO using the Jackson library. 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());
    }
}

其中ABC是我的POJO类,而abc是对象,但是出现以下异常:

where ABC is my POJO class and abc is the object but I get the following exception:

com.fasterxml.jackson.databind.JsonMappingException:无法反序列化com.demo.json.model.ABC实例

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.demo.json.model.ABC

我的POJO看起来像这样:

My POJO looks like this:

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

有人可以提出解决方案吗?

Can someone suggest a solution for this?

在StackOverflow和其他论坛上咨询了很多答案之后,我遇到了一个解决方案.我将readValue()方法的返回值映射到一个POJO对象数组中.

EDIT 2: After consulting a lot of answers on StackOverflow and other forums, I came across one solution. I mapped the returned value of readValue() method into an array of POJO objects.

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

但是现在我得到了一个单独的例外

But now I am getting a separate exception

无法构造ABC实例:没有长/长参数构造函数/工厂方法可从Number值反序列化(1552572583232)

Can not construct instance of ABC: no long/Long-argument constructor/factory method to deserialize from Number value (1552572583232)

我尝试了以下方法,但没有任何效果: 1.强制Jackson使用

I have tried the following but nothing worked: 1. Forcing Jackson to use ints for long values using

mapper.configure(DeserializationFeature.USE_LONG_FOR_INTS, true);

2.在POJO中使用包装器类Long代替long

2. Using wrapper class Long instead of long in the POJO

有人可以帮我吗?

推荐答案

您可以为该对象使用ARRAY形状.您可以使用JsonFormat注释:

You can use ARRAY shape for this object. You can do that using JsonFormat annotation:

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

并将其反序列化:

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());
    }
}

我们可以使用convertValue方法并跳过序列化过程:

We can use convertValue method and skip serializing process:

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());
    }
}

甚至:

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

这篇关于使用Jackson将Json Array映射到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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