2014-02-14 192 views
2

我从服务器创建这个json字符串,如下所示,我也能够解析字符串。但是当创建json像errormessage,createdDate等字段时,优先级为空值。我不想在字符串中显示它们,我该怎么做?Java到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", 
} 

通用的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; 
    } 

} 
+0

为什么你不想在JSON中有空值?它准确地反映了你的对象的状态。如何转换为JSON?可能你可以控制转换器的行为,但我们无法帮助,直到你确定它是什么。 – djna

+1

这完全取决于您使用的JSON序列化库。 –

+0

我正在使用json-20090211.jar传达给json字符串JSONObject jsonget = new JSONObject(appObject);和jsonget.tostring();返回字符串。 – jos

回答

1

老实说,我真的不会花时间让有效载荷“看起来不错”。现在,如果你说你出于效率原因而有意保持有效载荷小,我会购买。也许你使用杰克逊序列化 JSON以及(我不明白你为什么使用两个不同的库)。我认为this question显示杰克逊的空值治疗可以控制。

0

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

+0

以及在'something'实际上是'null'的情况下应该返回什么? – ingenious