简体   繁体   中英

Java to json String ignore null values

I am creating this json string from the server as shown below and I was able to parse the string too. But when creating the json some fields like errormessage,createdDate,priority are null values. I dont want to show them in the string how can I do that?

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

expected String

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

Generic Java Bean For creating the 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;
    }

}

Honestly, I really wouldn't spend time on making a payload "look nice". Now if you said that you were motivated to keep the payload small for efficiency reasons I'd buy that.

Perhaps you use Jackson for serialising to JSON as well (I don't see why you are using two different libraries). I think that this question shows that Jackson's treatment of nulls can be controlled.

Change your get method. Set if(something!=null)return something; instead of return something;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM