码迷,mamicode.com
首页 > Web开发 > 详细

json教程系列(3)-JSONObject的过滤设置

时间:2017-03-23 22:28:39      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:lte   port   instance   void   exclude   name   als   技术   view   

我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值。例如下面的类:

技术分享
 1 public class Person
 2 {
 3     private String name;
 4     private String address;
 5     private String sex;
 6  
 7     public String getAddress()
 8     {
 9         return address;
10     }
11  
12     public void setAddress(String address)
13     {
14         this.address = address;
15     }
16  
17     public String getName()
18     {
19         return name;
20     }
21  
22     public void setName(String name)
23     {
24         this.name = name;
25     }
26  
27     public String getSex()
28     {
29         return sex;
30     }
31  
32     public void setSex(String sex)
33     {
34         this.sex = sex;
35     }
36 }
View Code

如果我想过滤address属性怎么办?

方法一:实现JSONString接口

技术分享
 1 import net.sf.json.JSONString;
 2 public class Person implements JSONString
 3 {
 4     private String name;
 5     private String sex;
 6     private String address;
 7     public String toJSONString()
 8     {
 9         return "{\"name\":\"" + name + "\",\"sex\":\"" + sex + "\"}";
10     }
11     public String getAddress()
12     {
13         return address;
14     }
15     public void setAddress(String address)
16     {
17         this.address = address;
18     }
19     public String getName()
20     {
21         return name;
22     }
23     public void setName(String name)
24     {
25         this.name = name;
26     }
27     public String getSex()
28     {
29         return sex;
30     }
31     public void setSex(String sex)
32     {
33         this.sex = sex;
34     }
35 }
36 import net.sf.json.JSONObject;
37 public class Test {
38 public static void main(String args[]) {
39        Person person = new Person();
40         person.setName("swiftlet");
41         person.setSex("men");
42         person.setAddress("china");
43         JSONObject json = JSONObject.fromObject(person);
44         System.out.println(json.toString());
45     }
46 }
View Code

方法二:设置jsonconfig实例,对包含和需要排除的属性进行添加或删除。

技术分享
 1 import net.sf.json.JSONObject;
 2 import net.sf.json.JsonConfig;
 3 public class Test
 4 {
 5     public static void main(String args[])
 6     {
 7         Person person = new Person();
 8         person.setName("swiftlet");
 9         person.setSex("men");
10         person.setAddress("china");
11         JsonConfig jsonConfig = new JsonConfig();
12         jsonConfig.setExcludes(new String[]
13         { "address" });
14         JSONObject json = JSONObject.fromObject(person, jsonConfig);
15         System.out.println(json.toString());
16     }
17 }
View Code

方法三:使用propertyFilter实例过滤属性。

技术分享
 1 import net.sf.json.JSONObject;
 2 import net.sf.json.JsonConfig;
 3 import net.sf.json.util.PropertyFilter;
 4 public class Test
 5 {
 6     public static void main(String args[])
 7     {
 8         Person person = new Person();
 9         person.setName("swiftlet");
10         person.setSex("men");
11         person.setAddress("china");
12         JsonConfig jsonConfig = new JsonConfig();
13         jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
14             public boolean apply(Object source, String name, Object value)
15             {
16                 return source instanceof Person && name.equals("address");
17             }
18         });
19         JSONObject json = JSONObject.fromObject(person, jsonConfig);
20         System.out.println(json.toString());
21     }
22 }
View Code

 

json教程系列(3)-JSONObject的过滤设置

标签:lte   port   instance   void   exclude   name   als   技术   view   

原文地址:http://www.cnblogs.com/benpaobawoniu/p/6607550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!