我想从JSON响应输出中删除null和“”值数据[重复]

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

这个问题在这里已有答案:

这里我在Entity类上使用Annotation

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",
}

当我的Annotation无法解决这个问题时,如何解决它

java json
1个回答
0
投票

如果你想忽略null或“”值,那么你可以在@JsonInclude(Include.NON_NULL)@JsonInclude(Include.NON_EMPTY)上学。

你可以在下面找到示例代码:

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

}

您也可以根据您的使用情况在任何修复列上使用@JsonIgnore

© www.soinside.com 2019 - 2024. All rights reserved.