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