gpt4 book ai didi

java - Jackson:如何在 JSON 序列化中包含空值属性

转载 作者:行者123 更新时间:2023-12-04 01:49:00 25 4
gpt4 key购买 nike

我正在尝试使用我自己的 Java 实体类和 Jackson 注释生成 JSON 响应。两个键值必须是 null在我的 JSON 响应中,但如果我将它们设置为 null它们从 JSON 中消失。不幸的是,我仅限于 Jackson 1.9.13 版。
我已经尝试将值设置为 null , 使用

@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
我的响应实体:
public class Response {

@JsonProperty("data")
private Data data;

@JsonProperty("error")
private Error error;

public Data getData() {
return data;
}

public void setData(PgSoftAuthData data) {
this.data = data;
}

public Error getError() {
return error;
}

public void setError(PgSoftError error) {
this.error = error;
}
}
我正在尝试生成这样的响应:
Data data = new Data();
data.setName("Name");
data.setTime(null); // This key dissappear from JSON

Response responseSuccess = Response();
responseSuccess.setData(data);
responseSuccess.setError(null); // Error object disappear from JSON
我想得到以下回复:
{
"data" : {
"name" : "Name",
"time" : null
},
"error" : null
}

最佳答案

添加 JsonInclude到相关类(class) ResponseData
(使用@ThomasFritsch 评论更新)

@JsonInclude(JsonInclude.Include.ALWAYS)

Value that indicates that property is to be always included, independent of value of the property.

关于java - Jackson:如何在 JSON 序列化中包含空值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com