【问题标题】:Java to json String ignore null valuesJava到json字符串忽略空值
【发布时间】:2014-02-14 06:23:27
【问题描述】:

我正在从服务器创建这个 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",
 }

用于创建 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;
    }

}

【问题讨论】:

  • 你为什么不想在 JSON 中有空值?它准确地反映了您的对象的状态。到 JSON 的转换是如何完成的?您可能可以控制转换器的行为,但在您确定它是什么之前,我们无法提供帮助。
  • 这完全取决于您使用的 JSON 序列化库。
  • 我正在使用 json-20090211.jar 转换为 json 字符串 JSONObject jsonget = new JSONObject(appObject);和 jsonget.tostring();返回字符串。
  • 和 jackson 将字符串转换为 java。 AppObject&lt;Requirement&gt; appObjectnew = new ObjectMapper() .readValue( json, new TypeReference&lt;AppObject&lt;Requirement&gt;&gt;() { });
  • @djna 我想使用 json 的通用结构来执行我的所有 Web 服务调用,反之亦然。这个想法只是对客户端隐藏一些字段并看起来不错

标签: java json web-services


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 如果something实际上是null,应该返回什么?
    • 如果方法不是void,你必须总是返回一些东西
    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 2019-03-21
    • 2017-05-23
    • 1970-01-01
    • 2021-05-16
    • 2014-03-01
    • 2014-02-17
    • 2015-10-01
    相关资源
    最近更新 更多