加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用jsonObject转换json字符时过滤bean中空值节点

发布时间:2020-12-16 18:46:15 所属栏目:百科 来源:网络整理
导读:例:Testtest=newTest(); test.setId("1"); test.setName("zhangsan"); jsonObject.fromobject(test,Test.class).toString; 输出:{"id":"1","name":"zhangsan"} 假如不封装name属性,只封装id属性 Testtest=newTest(); test.setId("1"); jsonObject.fromobj

例:Testtest=newTest();
test.setId("1");
test.setName("zhangsan");
jsonObject.fromobject(test,Test.class).toString;
输出:{"id":"1","name":"zhangsan"}

假如不封装name属性,只封装id属性
Testtest=newTest();
test.setId("1");
jsonObject.fromobject(test,"name":""}
如果只输出:{"id":"1"}
这样的json字符,有什么方法可以解决么?


************************************************************

4
5
6
7
8
9
10
11
12
13
public static void main(String[]args){
Testt= new Test();
t.id= 10 ;
JsonConfigjsonConfig= new JsonConfig();
PropertyFilterfilter= new PropertyFilter(){
public boolean apply(Objectobject,StringfieldName,ObjectfieldValue){
return null ==fieldValue;
}
};
jsonConfig.setJsonPropertyFilter(filter);
System.out.println(JSONObject.fromObject(t,jsonConfig).toString());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读