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

 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 <T> 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> T deserialize(String json, Class<T> clz) {
24         return JSON.parseObject(json, clz);
25     }
26 
27     /**
28      * 将JSON格式的字符串转换为List<T>类型的对象
29      * @param json JSON格式的字符串
30      * @param clz 指定泛型集合里面的T类型
31      * @return List<T>类型的对象
32      */
33     public static <T> List<T> deserializeList(String json, Class<T> 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> T deserializeAny(String json, TypeReference<T> type) {
44         return JSON.parseObject(json, type);
45     }
46 
47 }
View Code
 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 }
View Code
 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 }
View Code
 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<Person> list = new ArrayList<Person>();
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 }
View Code
 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<Person> list = new ArrayList<Person>();
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<Person> 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 }
View Code
 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<String> list = new ArrayList<String>();
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 }
View Code
 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<String, Object> map = new HashMap<String, Object>();
17         map.put("key1", "value1");
18         map.put("key2", "value2");
19 
20         HashMap<String, Object> map2 = new HashMap<String, Object>();
21         map2.put("key1", 1);
22         map2.put("key2", 2);
23 
24         HashMap<String, Object> map3 = new HashMap<String, Object>();
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<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
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<List<HashMap<String, Object>>>() {
44                 });
45         for (HashMap<String, Object> item : list) {
46             for (Map.Entry<String, Object> 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 }
View Code

 

posted @ 2014-04-11 15:03  —八戒—  阅读(7300)  评论(0编辑  收藏  举报