zoukankan      html  css  js  c++  java
  • java不返回某些字段,包括 null

    一、使用 fastjson 包。

    (1) SimplePropertyPreFilter 可以将需要的字段留下来。

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;
    import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

    List<OffNews> list = offNewsDao.select(news); // // 需要的字段 填上 SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id","heading","url","urlValid","publishTime"); String result = JSONObject.toJSONString(list,filter); List<OffNews> usa2 = JSON.parseObject(result, new TypeReference<List<OffNews>>(){});

    (2)在类上打注解,。在通过将有用字符串转回对象后,其他字段均为null 了,就将所有null 字段不返回。

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    public class OffNews {
     
  • 相关阅读:
    python time 转换&运算tips
    Web.py session用户认证
    简单的内存池实现gko_alloc
    cpp(第十章)
    cpp(第九章)
    cpp(第八章)
    cpp(第七章)
    cpp(第六章)
    cpp(第五章)
    结构中的位字段
  • 原文地址:https://www.cnblogs.com/mobaids/p/11819448.html
Copyright © 2011-2022 走看看