json教程系列(3)-JSONObject的過濾設定

來源:互聯網
上載者:User

標籤: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的過濾設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.