FastJSON使用案例(fastjson-1.1.28.jar)

dawn009發表於2015-06-06
1 import java.util.List;
 2
 3 import com.alibaba.fastjson.JSON;
 4 import com.alibaba.fastjson.TypeReference;
 5
 6 public class FastJSONHelper {
 7
 8     /**
 9      * 將java型別的物件轉換為JSON格式的字串
10      * @param object java型別的物件
11      * @return JSON格式的字串
12      */
13     public static String serialize(T object) {
14         return JSON.toJSONString(object);
15     }
16
17     /**
18      * 將JSON格式的字串轉換為java型別的物件或者java陣列型別的物件,不包括java集合型別
19      * @param json JSON格式的字串
20      * @param clz java型別或者java陣列型別,不包括java集合型別
21      * @return java型別的物件或者java陣列型別的物件,不包括java集合型別的物件
22      */
23     public static T deserialize(String json, Class clz) {
24         return JSON.parseObject(json, clz);
25     }
26
27     /**
28      * 將JSON格式的字串轉換為List型別的物件
29      * @param json JSON格式的字串
30      * @param clz 指定泛型集合裡面的T型別
31      * @return List型別的物件
32      */
33     public static List deserializeList(String json, Class clz) {
34         return JSON.parseArray(json, clz);
35     }
36
37     /**
38      * 將JSON格式的字串轉換成任意Java型別的物件
39      * @param json JSON格式的字串
40      * @param type 任意Java型別
41      * @return 任意Java型別的物件
42      */
43     public static T deserializeAny(String json, TypeReference type) {
44         return JSON.parseObject(json, type);
45     }
46
47 }

複製程式碼
複製程式碼

 1 public class Person {
 2     private int Age;
 3     private String Name;
 4     public int getAge() {
 5         return Age;
 6     }
 7     public void setAge(int age) {
 8         Age = age;
 9     }
10     public String getName() {
11         return Name;
12     }
13     public void setName(String name) {
14         Name = name;
15     }
16 }

複製程式碼
複製程式碼

 1 public class Program1 {
 2
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Person person = new Person();
 9         person.setAge(32);
10         person.setName("wangyunpeng");
11         String json = FastJSONHelper.serialize(person);
12         System.out.println(json);
13         
14         person = FastJSONHelper.deserialize(json, Person.class);
15         System.out.println(String.format("Name:%s,Age:%s",person.getName(),person.getAge()));
16     }
17
18 }

複製程式碼
複製程式碼

 1 import java.util.ArrayList;
 2
 3 public class Program2 {
 4
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         
11         ArrayList list = new ArrayList();
12         Person person1 = new Person();
13         person1.setAge(32);
14         person1.setName("wangyunpeng");
15         list.add(person1);
16         Person person2 = new Person();
17         person2.setAge(17);
18         person2.setName("shyx");
19         list.add(person2);
20         String json = FastJSONHelper.serialize(list);
21         System.out.println(json);
22         
23         Person[] persons = FastJSONHelper.deserialize(json, Person[].class);
24         for (Person person : persons) {
25             System.out.println(String.format("Name:%s,Age:%s", person.getName(),person.getAge()));
26         }
27     }
28 }

複製程式碼
複製程式碼

 1 import java.util.ArrayList;
 2 import java.util.List;
 3
 4
 5 public class Program3 {
 6
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         ArrayList list = new ArrayList();
13         Person person1 = new Person();
14         person1.setAge(32);
15         person1.setName("wangyunpeng");
16         list.add(person1);
17         Person person2 = new Person();
18         person2.setAge(17);
19         person2.setName("shyx");
20         list.add(person2);
21         String json = FastJSONHelper.serialize(list);
22         System.out.println(json);
23         
24         List personList = FastJSONHelper.deserializeList(json, Person.class);
25         for (Person person : personList) {
26             System.out.println(String.format("Name:%s,Age:%s", person.getName(),person.getAge()));
27         }
28     }
29
30 }

複製程式碼
複製程式碼

 1 import java.util.ArrayList;
 2 import java.util.List;
 3
 4 public class Program4 {
 5
 6     /**
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         List list = new ArrayList();
12         list.add("wyp");
13         list.add("shyx");
14         String json = FastJSONHelper.serialize(list);
15         System.out.println(json);
16         list = FastJSONHelper.deserializeList(json, String.class);
17         for (String string : list) {
18             System.out.println(string);
19         }
20     }
21 }

複製程式碼
複製程式碼

 1 import java.util.ArrayList;
 2 import java.util.HashMap;
 3 import java.util.List;
 4 import java.util.Map;
 5
 6 import com.alibaba.fastjson.JSONObject;
 7 import com.alibaba.fastjson.TypeReference;
 8
 9 public class Program5 {
10
11     /**
12      * @param args
13      */
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16         HashMap map = new HashMap();
17         map.put("key1", "value1");
18         map.put("key2", "value2");
19
20         HashMap map2 = new HashMap();
21         map2.put("key1", 1);
22         map2.put("key2", 2);
23
24         HashMap map3 = new HashMap();
25         Person person1 = new Person();
26         person1.setAge(32);
27         person1.setName("wangyunpeng");
28         map3.put("wyp", person1);
29         Person person2 = new Person();
30         person2.setAge(17);
31         person2.setName("shenyunxiao");
32         map3.put("shyx", person2);
33
34         List> list = new ArrayList>();
35         list.add(map);
36         list.add(map2);
37         list.add(map3);
38
39         String json = FastJSONHelper.serialize(list);
40         System.out.println(json);
41
42         list = FastJSONHelper.deserializeAny(json,
43                 new TypeReference>>() {
44                 });
45         for (HashMap item : list) {
46             for (Map.Entry entry : item.entrySet()) {
47                 String key = entry.getKey();
48                 Object value = entry.getValue();
49                 if (value instanceof JSONObject) {
50                     JSONObject jObj = (JSONObject) value;
51                     String json2 = FastJSONHelper.serialize(jObj);
52                     Person other = FastJSONHelper.deserialize(json2, Person.class);
53                     System.out.println(String.format(
54                             "Key:%s,Value:[Name:%s,Age:%s]", key,
55                             other.getName(), other.getAge()));
56                 } else {
57                     System.out.println(String.format("Key:%s,Value:%s", key,
58                             value));
59                 }
60             }
61         }
62
63     }
64
65 }

出處:http://blog.csdn.net/lizzy115/article/details/38534823

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29119536/viewspace-1689593/,如需轉載,請註明出處,否則將追究法律責任。

相關文章