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 static <T> String serialize(T object) { 14 return JSON.toJSONString(object); 15 } 16 17 18 * 将JSON格式的字符串转换为java类型的对象或者java数组类型的对象,不包括java集合类型 19 json JSON格式的字符串 20 clz java类型或者java数组类型,不包括java集合类型 21 java类型的对象或者java数组类型的对象,不包括java集合类型的对象 22 23 static <T> T deserialize(String json,Class<T> clz) { 24 return JSON.parSEObject(json,clz); 25 } 26 27 28 * 将JSON格式的字符串转换为List<T>类型的对象 29 30 clz 指定泛型集合里面的T类型 31 List<T>类型的对象 32 33 static <T> List<T> deserializeList(String json,128); line-height:1.5!important">34 return JSON.parseArray(json,128); line-height:1.5!important">35 } 36 37 38 * 将JSON格式的字符串转换成任意Java类型的对象 39 40 type 任意Java类型 41 任意Java类型的对象 42 43 static <T> T deserializeAny(String json,TypeReference<T> type) { 44 45 } 46 47 }
class Person {
2 private int Age;
3 private String Name;
4 int getAge() {
5 return Age;
6 }
7 void setAge(int age) {
8 Age = age;
9 }
10 public String getName() {
11 return Name;
12 }
void setName(String name) {
14 Name = name;
16 }
class Program1 {
args
5 6 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 }
import java.util.ArrayList;
class Program2 {
4
5 7 9 10
11 ArrayList<Person> list = new ArrayList<Person>();
12 Person person1 = 13 person1.setAge(32);
14 person1.setName("wangyunpeng");
15 list.add(person1);
16 Person person2 = 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[].for (Person person : persons) {
25 System.out.println(String.format("Name:%s,128); line-height:1.5!important">26 }
27 }
28 }
2 3
5 class Program3 {
6
8 9 12 ArrayList<Person> list = 13 Person person1 = 14 person1.setAge(32);
15 person1.setName("wangyunpeng");
16 list.add(person1);
17 Person person2 = 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,128); line-height:1.5!important">25 for (Person person : personList) {
26 System.out.println(String.format("Name:%s,128); line-height:1.5!important">27 }
28 }
29
30 }
class Program4 {
7 8 9 10 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.17 for (String string : list) {
18 System.out.println(string);
19 }
20 }
21 }
原文链接:https://www.f2er.com/json/289787.html
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
8
class Program5 {
10
11 12 13 14 15 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 = 21 map2.put("key1",1);
22 map2.put("key2",2);
23
24 HashMap<String,Object> map3 = 25 Person person1 = 26 person1.setAge(32);
27 person1.setName("wangyunpeng");
28 map3.put("wyp",person1);
29 Person person2 = 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,128); line-height:1.5!important">53 System.out.println(String.format(
54 "Key:%s,Value:[Name:%s,Age:%s]",key,128); line-height:1.5!important">55 other.getName(),other.getAge()));
56 } else {
57 System.out.println(String.format("Key:%s,Value:%s",128); line-height:1.5!important">58 value));
59 }
60 }
61 }
62
63 }
64
65 }