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 {
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM