为什么杰克逊在 Json 中添加一个"empty":假



我用Jackson注释了一个Java类

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyOrder({
"estimated_shipping_weight",
"useestimatedweight",
"matched",
"available",
"primary_vendor",
"stock_status_code",
"primary_vendor",
"listprice",
"webprice",
"hits",
"IsIndividual",
"clearance",
"display_uom",
"display_uom_factor",
"weight",
"rank",
"box_qty",
"ddsFileDateSource",
"dateLastUpdated",
"ddiFileDateSource",
"cost"
})
public class AdditionalFields {
public static final Logger LOG = LoggerFactory.getLogger(AdditionalFields.class);
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("estimated_shipping_weight")
private Double estimatedShippingWeight;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("useestimatedweight")
private String useestimatedweight;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("matched")
private String matched;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("IsIndividual")
private String IsIndividual;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("stock_status_code")
private String stockStatusCode;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("available")
private Integer available;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("sch_available")
private String schAvailable;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("primary_vendor")
private Integer primaryVendor;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("listprice")
private Double listprice;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("webprice")
private Double webprice;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("hits")
private Integer hits;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("clearance")
private String clearance;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("display_uom_factor")
private Integer displayUomFactor;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("display_uom")
private String displayUom;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("weight")
private Double weight;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("box_qty")
private Integer boxQty;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("rank")
private Integer rank;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("product_line_description")
private String productLineDescription;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("ddiFileDateSource")
private String ddiFileDateSource;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("ddsFileDateSource")
private String ddsFileDateSource;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("dateLastUpdated")
private String dateLastUpdated;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("cost")
private String cost;

然而,我得到了一个额外的字段"empty": false,在json输出中是不需要的。我想知道为什么这个字段会出现在输出中,以及如何删除它。

"additional_fields": {
"matched": "f",
"available": 0,
"primary_vendor": 864,
"listprice": 1364.29,
"webprice": 1604.414,
"display_uom": "EA",
"display_uom_factor": 1,
"ddiFileDateSource": "DDI_2021_10_06_14_36_37.zip",
"empty": false,
"product_line_description": "ABB"
}

序列化
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
String json = mapper.writeValueAsString(additionalFields);

然而,我得到了一个额外的字段"empty": false,在json不需要的输出。我在想为什么那个字段会出现在输出和如何删除。

作为一个用户在他的评论中建议,这种行为是由于杰克逊库的序列化检查你的类,当它发现一个方法开头的前缀,它会自动添加一个字段在json文件中的名字(在这种情况下empty),出现在前缀和相应的值之后。为了避免这种情况,你可以用JsonIgnore来注释你的方法。

相关内容

最新更新