我從服務器創建這個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;
}
}
爲什麼你不想在JSON中有空值?它準確地反映了你的對象的狀態。如何轉換爲JSON?可能你可以控制轉換器的行爲,但我們無法幫助,直到你確定它是什麼。 – djna
這完全取決於您使用的JSON序列化庫。 –
我正在使用json-20090211.jar傳達給json字符串JSONObject jsonget = new JSONObject(appObject);和jsonget.tostring();返回字符串。 – jos