【问题标题】:I want to remove null and "" values data from JSON responce output [duplicate]我想从 JSON 响应输出中删除 null 和 "" 值数据 [重复]
【发布时间】:2018-01-07 20:59:09
【问题描述】:

这里我在实体类上使用注解

import java.util.ArrayList;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MappingDTO {

    private String mid; 
    private String location;
    private String department;
    private String role;
    private String tenent_id;
    private String cid;
    private ArrayList<CategoryDTO> categoryDTOAL;
    //setters and getters
}

并且使用 Jar 是

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version> 
</dependency>

我当前的输出

{
"mid":"1",
"location":"",
"department":"IT"
"role":"Developer",
"tenent_id":"",
"cid":"1001",
"categoryDTOAL":null
}

预期输出

 {
"mid":"1",
"department":"IT"
"role":"Developer",
"cid":"1001",
}

当我的这个注释无法解决这个问题时,我该如何解决 问题

【问题讨论】:

  • 终于我得到了答案 &lt;dependency&gt; &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt; &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt; &lt;version&gt;1.5.0&lt;/version&gt; &lt;/dependency&gt; 添加这个 jar 并使用注释 @JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY) 这里我正在导入 import org.codehaus.jackson.map.annotate.JsonSerialize;

标签: java json


【解决方案1】:

如果你想忽略 null 或 "" 值,那么你可以 tru @JsonInclude(Include.NON_NULL)@JsonInclude(Include.NON_EMPTY) 开始类。

您可以找到以下示例代码:

@JsonInclude(Include.NON_NULL) 
public static class Test {

}

您还可以根据您的用例在任何修复列上使用@JsonIgnore

【讨论】:

  • 那么 OP 的代码有什么问题?
  • 你好@Mayank Sharma 它不适用于我的情况....
  • 然后尝试使用@JsonSerialize(include = Inclusion.NON_NULL) 而不是@JsonInclude(JsonInclude.Include.NON_EMPTY) 或尝试使用@JsonInclude(JsonInclude.Include.NON_NULL)
  • @mayank Sharma 当我在“中间”中使用“@JsonIgnore”时,它会强制删除它。我只想删除 null 和 "" 值。
  • 实际上。 @JsonInclude(JsonSerialize.Inclusion.NON_NULL) 在我的代码中运行良好,根据文档版本 2.x+,此注释的语法为:@JsonInclude(JsonSerialize.Inclusion.NON_NULL),您也可以使用 2.6.0。它应该工作。你能把版本升级到2.8.8吗,也许它可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 2021-02-27
相关资源
最近更新 更多