Java转json字符串忽略空值

标签 java json web-services

我正在从服务器创建这个 json 字符串,如下所示,我也能够解析该字符串。但是在创建 json 时,一些字段如错误消息、创建日期、优先级是空值。我不想在字符串中显示它们,我该怎么做?

杰森斯特林

{
  "errormessage": null,
  "createdDate": null,
  "list": [{
  "type": "app1",
  "alternateId": "AlternateID",
  "priority": null,
  "description": "app for desc",
          }],
  "locationName": null,
  "facilityManagerName": null,
  "codeName": null,
  "sourceKey": null,
  "tablename": null,
  "path": "list",
  "service": "listserver",
  "license": null,
  "key": null,
 }

预期的字符串

 {
  "list": [{
  "type": "app1",
  "alternateId": "AlternateID",
  "description": "app for desc",
          }],
  "path": "list",
  "service": "listserver",
 }

用于创建 json 的通用 Java Bean:

public class AppObject<T> implements Serializable {
    private String errormessage;
    private Date createdDate;
    private List<T> list;
    private String locationName;
    private String facilityManagerName;
    private String codeName;
    private Long sourceKey;
    private String tablename;
    private String path;
    private String service;
    private String license;
    private Long key;

    public AppObject() {
        list = new ArrayList<T>();
    }

    public AppObject(List<T> list) {
        this.list = list;
    }

    @XmlAnyElement(lax = true)
    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }
    public String getTablename() {
        return tablename;
    }

    public void setTablename(String tablename) {
        this.tablename = tablename;
    }

    public String getErrormessage() {
        return errormessage;
    }

    public void setErrormessage(String errormessage) {
        this.errormessage = errormessage;
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }
    public String getLocationName() {
        return locationName;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }

    public String getFacilityManagerName() {
        return facilityManagerName;
    }

    public void setFacilityManagerName(String facilityManagerName) {
        this.facilityManagerName = facilityManagerName;
    }

    public Date getCreatedFeedFromDate() {
        return createdFeedFromDate;
    }

    @JsonDeserialize(using = com.vxl.JsonDateDeserializer.class)
    public void setCreatedFeedFromDate(Date createdFeedFromDate) {
        this.createdFeedFromDate = createdFeedFromDate;
    }

    public Date getCreatedDate() {
        return createdFeedToDate;
    }

    @JsonDeserialize(using = com.vxl.JsonDateDeserializer.class)
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

}

最佳答案

老实说,我真的不会花时间让有效负载“看起来不错”。现在,如果您说出于效率原因,您有动力保持较小的有效载荷,我会买账。

也许您也使用 Jackson 将 序列化为 JSON(我不明白您为什么使用两个不同的库)。我认为this question表明 Jackson 对 null 的处理是可以控制的。

关于Java转json字符串忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21772221/

相关文章:

json - PostgreSQL JSONB - 具有可变键名的查询条件

javascript - Firefox 14.0.1 在读取通过 AJAX 发送的 JSON 文件时返回错误

c# - 如何防止任何人向我的网络服务发送请求

c# - 将 WebClient 错误作为字符串获取

java - 连接客户端-服务器 RMI

java - 非捕获组正则表达式在 Java 中不起作用

java - JPA - 多列鉴别器

java - 将变量放入 json 文本正文中

ruby-on-rails - Rails 4、子域路由

java - 了解 'behaviors' 和方法的放置