Jackson - 如何使字段始终展开并包含

问题描述 投票:0回答:0

我正在尝试删除包装 json 数据的重复密钥。我曾尝试将

@JsonUnwrapped
@JsonInclude(JsonInclude.Include.ALWAYS)
一起使用,但没有运气。

我希望包括两个字段内容,无论它们是否为空/空,并删除重复的“

Item
”和“
Order
”包装内容。

如果我只使用

@JsonUnwrapped
,那会处理重复的“Item”和“Order”键。 但是,当该字段为空时,会发生展开并且不包含空
{}

比如如果

Order
为空,我还是想像这样包含在Json中

{
    "Item": {
        "111": [
            "item-one",
            "item-two",
        ],
    },
    "Order": {}
}

当前:

{
    "Item":{
        "Item": {
            "111": [
                "item-one",
                "item-two",
            ],
        }
    },
    "Order" : {
        "Order": {
            "222": "value1",
            "333": "value2"
        }
    }
}

期望的输出:

{
    "Item": {
        "111": [
            "item-one",
            "item-two",
        ],
    },
    "Order": {
        "222": "value1",
        "333": "value2"
    }
}

MyResponse 课程

@Data
@ToString
@AllArgsConstructor(staticName = "create")
public class MyResponse {
    
    @JsonUnwrapped
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private Item item;
    
    @JsonUnwrapped
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private Order order;
    
}

谢谢。

java json jackson response jackson2
© www.soinside.com 2019 - 2024. All rights reserved.