Java到json String忽略空值

时间:2014-02-14 06:23:28

标签: java json web-services

我正在从服务器创建这个json字符串,如下所示,我也能够解析字符串。但是在创建json时,一些字段如errormessage,createdDate,priority是空值。我不想在字符串中显示它们我该怎么做?

Json Strin

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

通用Java Bean用于创建json:

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;
    }

}

2 个答案:

答案 0 :(得分:1)

老实说,我真的不会花时间让有效载荷“看起来不错”。现在,如果你说你因为效率原因而有动力保持有效载荷很小的话我就买了。

也许你使用Jackson来序列化 JSON(我不明白你为什么要使用两个不同的库)。我认为this question表明杰克逊对空值的处理可以得到控制。

答案 1 :(得分:-1)

更改您的get方法。设置if(something!=null)return something;而不是return something;