首页 > 代码库 > json教程系列(3)-JSONObject的过滤设置

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

我们通常对一个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的过滤设置