Jackson - 将内部对象列表反序列化为更高级别的列表 [英] Jackson - deserialize inner list of objects to list of one higher level

查看:30
本文介绍了Jackson - 将内部对象列表反序列化为更高级别的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Spring Boot 和 Jackson,如何直接在外层将包装/内部列表反序列化为列表?

With Spring Boot and Jackson, how can I deserialize a wrapped/inner list into a list directly in the outer level?

例如,我有:

{
    "transaction": {
    "items": {
        "item": [
            {
                "itemNumber": "193487654",
                "itemDescription": "Widget",
                "itemPrice": "599.00",
                "itemQuantity": "1",
                "itemBrandName": "ACME",
                "itemCategory": "Electronics",
                "itemTax": "12.95"
            },
            {
                "itemNumber": "193487654",
                "itemDescription": "Widget",
                "itemPrice": "599.00",
                "itemQuantity": "1",
                "itemBrandName": "ACME",
                "itemCategory": "Electronics",
                "itemTax": "12.95"
            }
        ]
    },
    ...
    }
}

在JSON中,itemitems下的一个列表;但我想将它解析为一个名为 items 的列表,直接在 transaction 下,而不是定义一个包含名为 的列表的 DTO Items>项目.

In the JSON, item is a list under items; but I want to parse it as a list named items, directly under transaction, instead of defining a DTO Items which contains a list named item.

这可能吗?如何定义这个DTO Item?

Is this possible? How to define this DTO Item?

public class TrasactionDTO {
    private List<Item> items;
    ...
}

public class Item {

}

这个问题类似,但没有解决问题.使用 Jackson 反序列化包装列表

This question is similar but does not solve the problem. Deserialize wrapped list using Jackson

推荐答案

看来 @JsonUnwrapped 正是我需要的.

It seems that @JsonUnwrapped is what I need.

https://www.baeldung.com/jackson-annotations

@JsonUnwrapped 定义了在序列化/反序列化时应该展开/展平的值.

@JsonUnwrapped defines values that should be unwrapped/flattened when serialized/deserialized.

让我们看看它是如何工作的;我们将使用注释来解开属性名称:

Let's see exactly how that works; we'll use the annotation to unwrap the property name:

public class UnwrappedUser {
    public int id;
 
    @JsonUnwrapped
    public Name name;
 
    public static class Name {
        public String firstName;
        public String lastName;
    }
 }

现在让我们序列化这个类的一个实例:

Let's now serialize an instance of this class:

@Test
public void whenSerializingUsingJsonUnwrapped_thenCorrect()
  throws JsonProcessingException, ParseException {
    UnwrappedUser.Name name = new UnwrappedUser.Name("John", "Doe");
    UnwrappedUser user = new UnwrappedUser(1, name);
 
    String result = new ObjectMapper().writeValueAsString(user);
     
    assertThat(result, containsString("John"));
    assertThat(result, not(containsString("name")));
}

输出如下所示 - 静态嵌套类的字段与其他字段一起展开:

Here's how the output looks like – the fields of the static nested class unwrapped along with the other field:

{
    "id":1,
    "firstName":"John",
    "lastName":"Doe"
}

所以,它应该是这样的:

So, it should be something like:

public class TrasactionDTO {
    
    private List<Item> items;
    ...
}

public static class Item {
    @JsonUnwrapped
    private InnerItem innerItem;
    ...

}

public static class InnerItem {
    private String itemNumber;
    ...
}

这篇关于Jackson - 将内部对象列表反序列化为更高级别的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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