使用Jackson将Json Array映射到POJO

编程入门 行业动态 更新时间:2024-10-28 21:19:46
本文介绍了使用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

本文发布于:2023-08-01 09:09:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Json   Jackson   POJO   Array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!