我想删除null和""来自JSON的值数据响应输出

时间:2017-08-01 06:19:39

标签: java json

这里我在实体类

上使用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",
}
  

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

1 个答案:

答案 0 :(得分:0)

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

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

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

}

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