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

 

FastJSON使用案例(fastjson-1.1.28.jar),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。