json-lib库用法

json-lib依赖


json-lib-2.3-jdk15.jar
=======================
commons-collections-3.1.jar
commons-beanutils.jar
ezmorph-1.0.4.jar
commons-logging-1.1.jar
commons-lang-2.3.jar
xom-1.1.jar

 

view plain copy to clipboard print ?
  1. package json;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.commons.beanutils.PropertyUtils;  
  9.   
  10.   
  11. import net.sf.ezmorph.bean.MorphDynaBean;  
  12. import net.sf.json.JSON;  
  13. import net.sf.json.JSONArray;  
  14. import net.sf.json.JSONObject;  
  15. import net.sf.json.xml.XMLSerializer;  
  16.   
  17. public class JSONTest {  
  18.       
  19.     private static String jsonStr = "{/"id/": 1, /"name/": /"haha/", /"email/": [{/"id/": 1, /"address/": /"[email protected]/"}, {/"id/": 2, /"address/": /"[email protected]/"}]}";  
  20.       
  21.     public static class User {  
  22.           
  23.         private String name;  
  24.         private int age;  
  25.           
  26.         public User() {  
  27.               
  28.         }  
  29.           
  30.         public User(String n, int a) {  
  31.             this.name = n;  
  32.             this.age = a;  
  33.         }  
  34.           
  35.         public void setName(String n) {  
  36.             this.name = n;  
  37.         }  
  38.           
  39.         public void setAge(int a) {  
  40.             this.age = a;  
  41.         }  
  42.           
  43.         public String getName() {  
  44.             return name;  
  45.         }  
  46.           
  47.         public int getAge() {  
  48.             return age;  
  49.         }  
  50.           
  51.     }  
  52.       
  53.     private static User[] getUsers() {  
  54.         return new User[] {  
  55.                 new User("name1"11),  
  56.                 new User("name2"22),  
  57.                 new User("name3"33)  
  58.         };  
  59.     }  
  60.       
  61.     private static void printValueFromJsonStr() {  
  62.          System.out.println(jsonStr);  
  63.          JSONObject jsonObj = JSONObject.fromObject(jsonStr);  
  64.          String name = jsonObj.getString("name");  
  65.          System.out.println("name=" + name);  
  66.          JSONArray jsonArr = jsonObj.getJSONArray("email");  
  67.          for (int i = 0; i < jsonArr.size(); i++) {  
  68.              JSONObject jObj = jsonArr.getJSONObject(i);  
  69.              System.out.println("address=" + jObj.getString("address"));  
  70.          }  
  71.     }  
  72.       
  73.     private static void strToJsonArray() {  
  74.         String str = "['1','2','3']";  
  75.         JSONArray arr = JSONArray.fromObject(str);  
  76.         System.out.println("strToJsonArray=" + arr.toString());  
  77.     }  
  78.       
  79.     private static void printValueFromJsonStr1() {  
  80.         System.out.println(jsonStr);  
  81.         JSONObject jsonObj = JSONObject.fromObject(jsonStr);  
  82.         Object bean = JSONObject.toBean(jsonObj);  
  83.         try {  
  84.             String name = (String)PropertyUtils.getProperty(bean, "name");  
  85.             Object email = PropertyUtils.getIndexedProperty(bean, "email"1);  
  86.             String addr = (String)PropertyUtils.getProperty(email, "address");  
  87.             System.out.println("class=" + email.getClass() + ";addr=" + addr);  
  88.             MorphDynaBean eBean = (MorphDynaBean)email;  
  89.             System.out.println("printValueFromJsonStr1=" + name);  
  90.         } catch (Exception e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.     }  
  94.       
  95.     private static void strToBean() {  
  96.         String str = "{/"name/": /"haha/", /"age/": 26}";  
  97.         JSONObject obj = JSONObject.fromObject(str);  
  98.         User bean = (User)obj.toBean(obj, User.class);  
  99.         System.out.println("name=" + bean.getName() + ";age=" + bean.getAge());  
  100.     }  
  101.       
  102.     private static void mapToJson() {  
  103.         Map map = new HashMap();  
  104.         map.put("key1""value1");  
  105.         map.put("key2""value2");  
  106.         map.put("key3""value3");  
  107.         JSONObject jsonObj = JSONObject.fromObject(map);  
  108.         System.out.println("Map=" + jsonObj.toString());  
  109.     }  
  110.       
  111.     private static void arrayToJson() {  
  112.         User[] users = getUsers();  
  113.         JSONArray jsonArr = JSONArray.fromObject(users);  
  114.         System.out.println("Array=" + jsonArr.toString());  
  115.     }  
  116.       
  117.     private static void listToJson() {  
  118.         User[] users = getUsers();  
  119.         List data = new ArrayList();  
  120.         for (int i = 0; i < users.length; i++) {  
  121.             data.add(users[i]);  
  122.         }  
  123.         JSONArray jsonArr = JSONArray.fromObject(data);  
  124.         System.out.println("List=" + jsonArr.toString());  
  125.     }  
  126.       
  127.     private static void objectToJson() {  
  128.         User user = new User("name"26);  
  129.         JSONObject jsonObj = JSONObject.fromObject(user);  
  130.         System.out.println("Object=" + jsonObj.toString());  
  131.     }  
  132.       
  133.     private static void combineToJson() {  
  134.         User[] users = getUsers();  
  135.         Map map = new HashMap();  
  136.         map.put("users", users);  
  137.         map.put("key1""value1");  
  138.         List list = new ArrayList();  
  139.         list.add(map);  
  140.         list.add("test");  
  141.         Map outerMap = new HashMap();  
  142.         outerMap.put("list", list);  
  143.         outerMap.put("flag""outer");  
  144.         JSONObject jsonObj = JSONObject.fromObject(outerMap);  
  145.         System.out.println("Combine=" + jsonObj.toString());  
  146.     }  
  147.       
  148.     private static void jsonToXml() {  
  149.         System.out.println(jsonStr);  
  150.         JSONObject jsonObj = JSONObject.fromObject(jsonStr);  
  151.         XMLSerializer x = new XMLSerializer();  
  152.         String xml = x.write(jsonObj);  
  153.         System.out.println("toXml=" + xml);  
  154.     }  
  155.       
  156.     private static void xmlToJson() {  
  157.         String xml = "[email protected]1";  
  158.         XMLSerializer x = new XMLSerializer();  
  159.         JSON json = x.read(xml);   
  160.         System.out.println("toJson=" + json.toString());  
  161.     }  
  162.       
  163.     public static void main(String[] args) {  
  164.         printValueFromJsonStr();  
  165.         printValueFromJsonStr1();  
  166.         strToJsonArray();  
  167.         strToBean();  
  168.         mapToJson();  
  169.         arrayToJson();  
  170.         listToJson();  
  171.         objectToJson();  
  172.         combineToJson();  
  173.         jsonToXml();  
  174.         xmlToJson();  
  175.     }  
  176.       
  177. }  
  178.   

     

    printValueFromJsonStr方法结果:

    {"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected]"}, {"id": 2, "address": "[email protected]"}]}

    name=haha
    [email protected]
    [email protected]

    printValueFromJsonStr1方法结果:

    {"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected]"}, {"id": 2, "address": "[email protected]"}]}
    class=class net.sf.ezmorph.bean.MorphDynaBean;[email protected]
    printValueFromJsonStr1=haha

    strToJsonArray方法结果:

    strToJsonArray=["1","2","3"]

    strToBean结果:

    name=haha;age=26

    mapToJson结果:

    Map={"key3":"value3","key2":"value2","key1":"value1"}

    arrayToJson结果:

    Array=[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}]

    listToJson结果:

    List=[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}]

    objectToJson结果:

    Object={"age":26,"name":"name"}

    combineToJson结果:

    Combine={"flag":"outer","list":[{"users":[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}],"key1":"value1"},"test"]}

    jsonToXml结果:

    {"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected]"}, {"id": 2, "address": "[email protected]"}]}
    toXml=

    [email protected]
    1
    [email protected]
    2
    1haha

    xmlToJson结果:

    toJson={"email":[{"address":"[email protected]","id":1}]}

    你可能感兴趣的:(技术文章)