fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map

直接给代码:

[java]  view plain copy
  1. package fastJson.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import com.alibaba.fastjson.JSON;  
  9. import com.alibaba.fastjson.JSONArray;  
  10.   
  11. public class FastJsonTest1  
  12. {  
  13.   
  14.     /** 
  15.      * 数组转json格式字符串 
  16.      */  
  17.     public void array2Json(){  
  18.         String[] arr = {"bill","green","maks","jim"};  
  19.         String jsonText = JSON.toJSONString(arr, true);  
  20.         System.out.println("array2Json()方法:jsonText=="+jsonText);  
  21.         // 输出结果:jsonText==["bill","green","maks","jim"]  
  22.     }  
  23.   
  24.     /** 
  25.      * json格式字符串转数组 
  26.      */  
  27.     public void json2Array(){  
  28.         String jsonText = "[\"bill\",\"green\",\"maks\",\"jim\"]";  
  29.         JSONArray jsonArr = JSON.parseArray(jsonText);  
  30.         System.out.println("json2Array()方法:jsonArr=="+jsonArr);  
  31.         // 输出结果:jsonArr==["bill","green","maks","jim"]  
  32.     }  
  33.       
  34.     /** 
  35.      * 数组转json格式字符串 
  36.      */  
  37.     public void array2Json2(){  
  38.         User user1 = new User("P001","TOM",16);  
  39.         User user2 = new User("P002","JACKSON",21);  
  40.         User user3 = new User("P003","MARTIN",20);  
  41.         User[] userArr = {user1,user2,user3};  
  42.         String jsonText = JSON.toJSONString(userArr, true);  
  43.         System.out.println("array2Json2()方法:jsonText=="+jsonText);  
  44.         //输出结果:jsonText==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]  
  45.     }  
  46.       
  47.     /** 
  48.      * json格式字符串转数组 
  49.      */  
  50.     public void json2Array2(){  
  51.         String jsonText = "[{\"age\":16,\"id\":\"P001\",\"name\":\"TOM\"},{\"age\":21,\"id\":\"P002\",\"name\":\"JACKSON\"},{\"age\":20,\"id\":\"P003\",\"name\":\"MARTIN\"}]";  
  52.         JSONArray jsonArr = JSON.parseArray(jsonText);  
  53.         System.out.println("json2Array2()方法:jsonArr=="+jsonArr);  
  54.         // 输出结果:jsonArr==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]  
  55.     }  
  56.       
  57.     /** 
  58.      * list集合转json格式字符串 
  59.      */  
  60.     public void list2Json(){  
  61.         List list = new ArrayList();  
  62.         User user1 = new User("L001","TOM",16);  
  63.         list.add(user1);  
  64.         User user2 = new User("L002","JACKSON",21);  
  65.         list.add(user2);  
  66.         User user3 = new User("L003","MARTIN",20);  
  67.         list.add(user3);  
  68.         String jsonText = JSON.toJSONString(list, true);  
  69.         System.out.println("list2Json()方法:jsonText=="+jsonText);  
  70.         //输出结果:jsonText==[{"age":16,"id":"L001","name":"TOM"},{"age":21,"id":"L002","name":"JACKSON"},{"age":20,"id":"L003","name":"MARTIN"}]  
  71.     }  
  72.       
  73.     /** 
  74.      * list集合转json格式字符串 
  75.      */  
  76.     public void list2Json2(){  
  77.         List list = new ArrayList();  
  78.         Address address1 = new Address("广东省","深圳市","科苑南路","580053");  
  79.         User user1 = new User("L001","TOM",16,address1);  
  80.         list.add(user1);  
  81.         Address address2 = new Address("江西省","南昌市","阳明路","330004");  
  82.         User user2 = new User("L002","JACKSON",21,address2);  
  83.         list.add(user2);  
  84.         Address address3 = new Address("陕西省","西安市","长安南路","710114");  
  85.         User user3 = new User("L003","MARTIN",20,address3);  
  86.         list.add(user3);  
  87.         String jsonText = JSON.toJSONString(list, true);  
  88.         System.out.println("list2Json2()方法:jsonText=="+jsonText);  
  89.         //输出结果:jsonText==[{"address":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"age":16,"id":"L001","name":"TOM"},{"address":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"age":21,"id":"L002","name":"JACKSON"},{"address":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"},"age":20,"id":"L003","name":"MARTIN"}]  
  90.     }  
  91.   
  92.     /** 
  93.      * map转json格式字符串 
  94.      */  
  95.     public void map2Json(){  
  96.         Map map = new HashMap();  
  97.         Address address1 = new Address("广东省","深圳市","科苑南路","580053");  
  98.         map.put("address1", address1);  
  99.         Address address2 = new Address("江西省","南昌市","阳明路","330004");  
  100.         map.put("address2", address2);  
  101.         Address address3 = new Address("陕西省","西安市","长安南路","710114");  
  102.         map.put("address3", address3);  
  103.         String jsonText = JSON.toJSONString(map, true);  
  104.         System.out.println("map2Json()方法:jsonText=="+jsonText);  
  105.         //输出结果:jsonText=={"address1":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"address2":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"address3":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"}}  
  106.     }  
  107.       
  108.       
  109.       
  110.     public static void main(String[] args)  
  111.     {  
  112.         FastJsonTest1 test = new FastJsonTest1();  
  113.         //数组转json格式字符串  
  114.         test.array2Json();  
  115.           
  116.         //json格式字符串转数组  
  117.         test.json2Array();  
  118.           
  119.         //数组转json格式字符串  
  120.         test.array2Json2();  
  121.           
  122.         //json格式字符串转数组  
  123.         test.json2Array2();  
  124.           
  125.         //list集合转json格式字符串  
  126.         test.list2Json();  
  127.           
  128.         //list集合转json格式字符串  
  129.         test.list2Json2();  
  130.           
  131.         //map转json格式字符串  
  132.         test.map2Json();  
  133.     }  
  134. }  

控制台输出结果:

[html]  view plain copy
  1. array2Json()方法:jsonText==["bill","green","maks","jim"]  
  2. json2Array()方法:jsonArr==["bill","green","maks","jim"]  
  3. array2Json2()方法:jsonText==[{  
  4.     "age":16,  
  5.     "id":"P001",  
  6.     "name":"TOM"  
  7. },{  
  8.     "age":21,  
  9.     "id":"P002",  
  10.     "name":"JACKSON"  
  11. },{  
  12.     "age":20,  
  13.     "id":"P003",  
  14.     "name":"MARTIN"  
  15. }]  
  16. json2Array2()方法:jsonArr==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]  
  17. list2Json()方法:jsonText==[  
  18.     {  
  19.         "age":16,  
  20.         "id":"L001",  
  21.         "name":"TOM"  
  22.     },  
  23.     {  
  24.         "age":21,  
  25.         "id":"L002",  
  26.         "name":"JACKSON"  
  27.     },  
  28.     {  
  29.         "age":20,  
  30.         "id":"L003",  
  31.         "name":"MARTIN"  
  32.     }  
  33. ]  
  34. list2Json2()方法:jsonText==[  
  35.     {  
  36.         "address":{  
  37.             "city":"深圳市",  
  38.             "post":"580053",  
  39.             "province":"广东省",  
  40.             "street":"科苑南路"  
  41.         },  
  42.         "age":16,  
  43.         "id":"L001",  
  44.         "name":"TOM"  
  45.     },  
  46.     {  
  47.         "address":{  
  48.             "city":"南昌市",  
  49.             "post":"330004",  
  50.             "province":"江西省",  
  51.             "street":"阳明路"  
  52.         },  
  53.         "age":21,  
  54.         "id":"L002",  
  55.         "name":"JACKSON"  
  56.     },  
  57.     {  
  58.         "address":{  
  59.             "city":"西安市",  
  60.             "post":"710114",  
  61.             "province":"陕西省",  
  62.             "street":"长安南路"  
  63.         },  
  64.         "age":20,  
  65.         "id":"L003",  
  66.         "name":"MARTIN"  
  67.     }  
  68. ]  
  69. map2Json()方法:jsonText=={"address1":{  
  70.     "city":"深圳市",  
  71.     "post":"580053",  
  72.     "province":"广东省",  
  73.     "street":"科苑南路"  
  74. },"address2":{  
  75.     "city":"南昌市",  
  76.     "post":"330004",  
  77.     "province":"江西省",  
  78.     "street":"阳明路"  
  79. },"address3":{  
  80.     "city":"西安市",  
  81.     "post":"710114",  
  82.     "province":"陕西省",  
  83.     "street":"长安南路"  
  84. }}  


附:javabean类 User.java

[java]  view plain copy
  1. package fastJson.test;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class User implements Serializable {  
  6.       
  7.     private static final long serialVersionUID = 1L;  
  8.       
  9.     private String id;  
  10.     private String name;  
  11.     private int age;  
  12.     private Address address;  
  13.       
  14.     public User() {  
  15.         super();  
  16.     }  
  17.   
  18.     public User(String id, String name, int age) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.name = name;  
  22.         this.age = age;  
  23.     }  
  24.       
  25.     public User(String id, String name, int age, Address address)  
  26.     {  
  27.         super();  
  28.         this.id = id;  
  29.         this.name = name;  
  30.         this.age = age;  
  31.         this.address = address;  
  32.     }  
  33.   
  34.     public int getAge() {  
  35.         return age;  
  36.     }  
  37.   
  38.     public void setAge(int age) {  
  39.         this.age = age;  
  40.     }  
  41.   
  42.     public String getId() {  
  43.         return id;  
  44.     }  
  45.   
  46.     public void setId(String id) {  
  47.         this.id = id;  
  48.     }  
  49.   
  50.     public String getName() {  
  51.         return name;  
  52.     }  
  53.   
  54.     public void setName(String name) {  
  55.         this.name = name;  
  56.     }  
  57.   
  58.     public Address getAddress()  
  59.     {  
  60.         return address;  
  61.     }  
  62.   
  63.     public void setAddress(Address address)  
  64.     {  
  65.         this.address = address;  
  66.     }  
  67.       
  68. }  

Address.java

[java]  view plain copy
  1. package fastJson.test;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Address implements Serializable  
  6. {  
  7.     private static final long serialVersionUID = 1L;  
  8.       
  9.     private String province;  
  10.     private String city;  
  11.     private String street;  
  12.     private String post;  
  13.       
  14.     public Address()  
  15.     {  
  16.         super();  
  17.     }  
  18.       
  19.     public Address(String province, String city, String street, String post)  
  20.     {  
  21.         super();  
  22.         this.province = province;  
  23.         this.city = city;  
  24.         this.street = street;  
  25.         this.post = post;  
  26.     }  
  27.   
  28.     public String getCity()  
  29.     {  
  30.         return city;  
  31.     }  
  32.   
  33.     public void setCity(String city)  
  34.     {  
  35.         this.city = city;  
  36.     }  
  37.   
  38.     public String getPost()  
  39.     {  
  40.         return post;  
  41.     }  
  42.   
  43.     public void setPost(String post)  
  44.     {  
  45.         this.post = post;  
  46.     }  
  47.   
  48.     public String getProvince()  
  49.     {  
  50.         return province;  
  51.     }  
  52.   
  53.     public void setProvince(String province)  
  54.     {  
  55.         this.province = province;  
  56.     }  
  57.   
  58.     public String getStreet()  
  59.     {  
  60.         return street;  
  61.     }  
  62.   
  63.     public void setStreet(String street)  
  64.     {  
  65.         this.street = street;  
  66.     }  
  67.       
  68. }  

你可能感兴趣的:(fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map)