一、各个JSON技术的对比

1.json-lib

json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,

包括commons-beanutils-1.7.0.jar,commons-collections-3.2.jar,commons-lang-2.4.jar,commons-logging-1.1.jar,ezmorph-1.0.4.jar,jdom.jar,json-lib-2.2.2-jdk15.jar

对于复杂类型的转换,json-lib对于json转换成bean还有缺陷,比如一个类里面会出现另一个类的list或者map集合,json-lib从json到bean的转换就会出现问题。

json-lib在功能和性能上面都不能满足现在互联网化的需求。

2.开源的Jackson

相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。

而且Jackson社区相对比较活跃,更新速度也比较快。

Jackson对于复杂类型的json转换bean会出现问题,一些集合Map,List的转换出现问题。

Jackson对于复杂类型的bean转换Json,转换的json格式不是标准的Json格式

3.Google的Gson

Gson是目前功能最全的Json解析神器,Gson当初是为因应Google公司内部需求而由Google自行研发而来,

但自从在2008年五月公开发布第一版后已被许多公司或用户应用。

Gson的应用主要为toJson与fromJson两个转换函数,无依赖,不需要例外额外的jar,只依赖于gson-2.7.jar,能够直接跑在JDK上。

而在使用这种对象转换之前需先创建好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。

类里面只要有get和set方法,Gson完全可以将复杂类型的json到bean或bean到json的转换,是JSON解析的神器。

Gson在功能上面无可挑剔,但是性能上面比FastJson有所差距。

4.阿里巴巴的FastJson

Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。

无依赖,不需要例外额外的jar,只需要fastjson-1.2.8.jar,能够直接跑在JDK上。

FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,导致Json转换出错,需要制定引用。

FastJson采用独创的算法,将parse的速度提升到极致,超过所有json库。


综上4种Json技术的比较,在项目选型的时候可以使用Google的Gson和阿里巴巴的FastJson两种并行使用,

如果只是功能要求,没有性能要求,可以使用google的Gson,

如果有性能上面的要求可以使用Gson将bean转换json确保数据的正确,使用FastJson将Json转换Bean


二、实例

1.首先建立实体Bean

(1)学生实体类

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.model;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 /**
7 * @ClassName: Student
8 * @Description: TODO(学生实体类)
9 * @author xbq
10 * @date 2017-2-2 上午9:31:06
11 *
12 */
13 public class Student implements Serializable{
14
15 /**
16 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
17 */
18 private static final long serialVersionUID = 1L;
19
20 private int id;
21 private String name;
22 private Date birth;
23 private String address;
24
25 public Student() {
26 super();
27 }
28
29 public Student(int id, String name, Date birth, String address) {
30 super();
31 this.id = id;
32 this.name = name;
33 this.birth = birth;
34 this.address = address;
35 }
36
37 public int getId() {
38 return id;
39 }
40 public void setId(int id) {
41 this.id = id;
42 }
43 public String getName() {
44 return name;
45 }
46 public void setName(String name) {
47 this.name = name;
48 }
49 public Date getBirth() {
50 return birth;
51 }
52 public void setBirth(Date birth) {
53 this.birth = birth;
54 }
55 public String getAddress() {
56 return address;
57 }
58 public void setAddress(String address) {
59 this.address = address;
60 }
61
62 @Override
63 public String toString() {
64 return "Student [id=" + id + ", name=" + name + ", birth=" + birth
65 + ", address=" + address + "]";
66 }
67
68 }

View Code

(2)包含学生List的实体类

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.model;
2
3 import java.io.Serializable;
4 import java.util.List;
5
6 /**
7 *
8 * @ClassName: StudentAnalysis
9 * @Description: TODO(包含学生 实体类 的List的 实体类)
10 * @author xbq
11 * @date 2017-2-2 上午9:31:46
12 *
13 */
14 public class StudentAnalysis implements Serializable{
15
16 /**
17 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
18 */
19 private static final long serialVersionUID = 1L;
20 private List<Student> students;
21
22 public StudentAnalysis() {
23 super();
24 }
25
26 public StudentAnalysis(List<Student> students) {
27 super();
28 this.students = students;
29 }
30
31 public List<Student> getStudents() {
32 return students;
33 }
34 public void setStudents(List<Student> students) {
35 this.students = students;
36 }
37
38 @Override
39 public String toString() {
40 return "StudentAnalysis [students=" + students + "]";
41 }
42
43 }

View Code


2.Gson测试类和封装的工具类

(1)测试类

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.gson.test;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.junit.Test;
10
11 import com.google.gson.Gson;
12 import com.google.gson.GsonBuilder;
13 import com.google.gson.reflect.TypeToken;
14 import com.model.Student;
15 import com.model.StudentAnalysis;
16
17 /**
18 * @ClassName: TestGson
19 * @Description: TODO(这里用一句话描述这个类的作用)
20 * @author xbq
21 * @date 2017-2-2 上午9:37:46
22 * Gson将 实体中的 Timestamp 类型 在 json中转化为 Feb 2, 2017 9:36:41 AM 形式
23 * Date 类型 在 json中转化为 Feb 2, 2017 9:36:41 AM 形式
24 */
25 public class TestGson {
26
27 /**
28 * @Title: testObjectToJson
29 * @Description: TODO(对象转Json)
30 * @param 设定文件
31 * @return void 返回类型
32 * @throws
33 */
34 @Test
35 public void testObjectToJson(){
36 Gson gson = new GsonBuilder().serializeNulls().create(); // 这样实例化 会将 实体中空的属性 在 json中显示为 null
37 Student student = new Student(10,"张三",new Date(),null);
38 String json = gson.toJson(student);
39 System.out.println("json==" + json);
40
41 List<Student> list = new ArrayList<Student>();
42 student = new Student(1,"张三",new Date(),null);
43 list.add(student);
44 student = new Student(2,"李四",new Date(),null);
45 list.add(student);
46 student = new Student(3,"王五",null,"山东");
47 list.add(student);
48 StudentAnalysis analysis = new StudentAnalysis(list);
49 String studentsJson = gson.toJson(analysis);
50 System.out.println("json==" + studentsJson);
51
52 }
53
54
55 @Test
56 public void jsonToObject(){
57 Gson gson = new GsonBuilder().serializeNulls().create();
58 String stuJson = "{\"id\":10,\"name\":\"张三\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"createDate\":\"Feb 2, 2017 9:46:39 AM\",\"address\":null}";
59 Student stu = gson.fromJson(stuJson, Student.class);
60 System.out.println("student==" + stu);
61
62 String stuListJson = "{\"students\":[{\"id\":1,\"name\":\"张三\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"createDate\":\"Feb 2, 2017 9:46:39 AM\",\"address\":null},{\"id\":2,\"name\":\"李四\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"createDate\":null,\"address\":null},{\"id\":3,\"name\":\"王五\",\"birth\":null,\"createDate\":\"Feb 2, 2017 9:46:39 AM\",\"address\":\"山东\"}]}";
63 StudentAnalysis analysis = gson.fromJson(stuListJson, StudentAnalysis.class);
64 System.out.println("studentList==" + analysis);
65
66 // 遍历List
67 for(Student s : analysis.getStudents()){
68 System.out.println(s);
69 }
70
71 String studentsJson = gson.toJson(analysis);
72 System.out.println("json==" + studentsJson);
73
74 }
75
76 @Test
77 public void ListToJson(){
78 Gson gson = new GsonBuilder().serializeNulls().create();
79
80 List<Student> list = new ArrayList<Student>();
81 Student student = new Student(1,"张三",new Date(),null);
82 list.add(student);
83 student = new Student(2,"李四",new Date(),null);
84 list.add(student);
85 student = new Student(3,"王五",null,"山东");
86 list.add(student);
87 // list 转为 json
88 String json = gson.toJson(list);
89 System.out.println(json);
90
91 // json转为 List
92 List<Student> students = gson.fromJson(json, new TypeToken<List<Student>>(){}.getType());
93 System.out.println(students);
94 }
95
96 @Test
97 public void MapToJson(){
98 Gson gson = new GsonBuilder().serializeNulls().create();
99
100 Map<String,List<Student>> map = new HashMap<String,List<Student>>();
101 List<Student> list1 = new ArrayList<Student>();
102 Student student = new Student(1,"张三",new Date(),null);
103 list1.add(student);
104 student = new Student(2,"李四",new Date(),null);
105 list1.add(student);
106 student = new Student(3,"王五",null,"山东");
107 list1.add(student);
108
109 List<Student> list2 = new ArrayList<Student>();
110 student = new Student(4,"张三2",new Date(),null);
111 list2.add(student);
112 student = new Student(5,"李四2",new Date(),null);
113 list2.add(student);
114 student = new Student(6,"王五2",null,"山东");
115 list2.add(student);
116
117 List<Student> list3 = new ArrayList<Student>();
118 student = new Student(7,"张三2",new Date(),null);
119 list3.add(student);
120 student = new Student(8,"李四2",new Date(),null);
121 list3.add(student);
122 student = new Student(9,"王五2",null,"山东");
123 list3.add(student);
124
125 map.put("list1", list1);
126 map.put("list2", list2);
127 map.put("list3", list3);
128
129 // map转为 json
130 String json = gson.toJson(map);
131 System.out.println(json);
132
133 // json转为map
134 map = gson.fromJson(json, new TypeToken<HashMap<String,List<Student>>>(){}.getType());
135 System.out.println(map);
136 }
137
138 }

View Code


(2)工具类(在项目中可以直接使用)

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.gson.util;
2
3 import java.lang.reflect.Type;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import com.google.gson.Gson;
11 import com.google.gson.GsonBuilder;
12 import com.google.gson.reflect.TypeToken;
13 import com.model.Student;
14 import com.model.StudentAnalysis;
15
16 /**
17 * @ClassName: GsonUtil
18 * @Description: TODO(这里用一句话描述这个类的作用)
19 * @author xbq
20 * @date 2017-2-2 下午12:42:59
21 *Gson将 实体中的 Timestamp 类型 在 json中转化为 Feb 2, 2017 9:36:41 AM 形式
22 * Date 类型 在 json中转化为 Feb 2, 2017 9:36:41 AM 形式
23 */
24 public class GsonUtil {
25
26 private static Gson gson = null;
27
28 static {
29 gson = new GsonBuilder().serializeNulls().create(); // 这样实例化 会将 实体中空的属性 在 json中显示为 null
30 }
31
32 /**
33 * @Title: objectToJson
34 * @Description: TODO(实体类、List、Map 转为 Json)
35 * @param @param obj
36 * @param @return 设定文件
37 * @return String 返回类型
38 * @throws
39 */
40 public static String objectToJson(Object obj){
41 return gson.toJson(obj);
42 }
43
44 /**
45 * @Title: jsonToObject
46 * @Description: TODO(Json转化为实体类 或 List、Map)
47 * @param @param json
48 * @param @param type
49 * @param @return 设定文件
50 * @return Object 返回类型
51 * @throws
52 */
53 public static Object jsonToObject(String json,Type type){
54 if(type instanceof List){
55 return gson.fromJson(json, new TypeToken<List<Type>>(){}.getType());
56 }else if(type instanceof Map){
57 return gson.fromJson(json, new TypeToken<HashMap<String,List<Type>>>(){}.getType());
58 }else {
59 return gson.fromJson(json, type);
60 }
61 }
62
63
64 /**
65 * 测试
66 * @Title: main
67 * @Description: TODO(这里用一句话描述这个方法的作用)
68 * @param @param args 设定文件
69 * @return void 返回类型
70 * @throws
71 */
72 public static void main(String[] args) {
73 Student student = new Student(10,"张三",new Date(),null);
74 String json = objectToJson(student);
75 System.out.println("---1---学生Student实体转为的Json------");
76 System.out.println(json);
77
78 List<Student> list = new ArrayList<Student>();
79 student = new Student(1,"张三",new Date(),null);
80 list.add(student);
81 student = new Student(2,"李四",new Date(),null);
82 list.add(student);
83 student = new Student(3,"王五",null,"山东");
84 list.add(student);
85 StudentAnalysis analysis = new StudentAnalysis(list);
86 String studentsJson = objectToJson(analysis);
87 System.out.println("---2----学生StudentAnalysis实体转为的Json------");
88 System.out.println(studentsJson);
89
90
91 String stuJson = "{\"id\":10,\"name\":\"张三\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"address\":null}";
92 Student stu = (Student) jsonToObject(stuJson, Student.class);
93 System.out.println("---3----学生Json转化为Student实体-------");
94 System.out.println(stu);
95
96 String stuListJson = "{\"students\":[{\"id\":1,\"name\":\"张三\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"address\":null},{\"id\":2,\"name\":\"李四\",\"birth\":\"Feb 2, 2017 9:46:39 AM\",\"createDate\":null,\"address\":null},{\"id\":3,\"name\":\"王五\",\"birth\":null,\"createDate\":\"Feb 2, 2017 9:46:39 AM\",\"address\":\"山东\"}]}";
97 StudentAnalysis analysis1 = (StudentAnalysis) jsonToObject(stuListJson, StudentAnalysis.class);
98 System.out.println("---4----学生analysis1转化为StudentAnalysis实体-------");
99 System.out.println(analysis1);
100
101 System.out.println("---5----遍历学生List--------");
102 // 遍历List
103 for(Student s : analysis1.getStudents()){
104 System.out.println(s);
105 }
106
107 }
108 }

View Code


3.FastJson测试类和封装的工具类

(1)测试类

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.fastjson.test;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.junit.Test;
10
11 import com.alibaba.fastjson.JSON;
12 import com.alibaba.fastjson.TypeReference;
13 import com.alibaba.fastjson.serializer.SerializerFeature;
14 import com.model.Student;
15 import com.model.StudentAnalysis;
16
17 /**
18 * fastjson 将 Date 和 Timestamp 类型的 转化为 long的形式
19 * @ClassName: TestFastJson
20 * @Description: TODO(这里用一句话描述这个类的作用)
21 * @author xbq
22 * @date 2017-2-2 下午2:00:27
23 *
24 */
25 public class TestFastJson {
26
27 @Test
28 public void ObjectToJson(){
29 Student student = new Student(10,"张三",new Date(),null);
30 // 缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出
31 String json = JSON.toJSONString(student,SerializerFeature.WriteMapNullValue);
32 System.out.println(json);
33
34 List<Student> list = new ArrayList<Student>();
35 student = new Student(1,"张三",new Date(),null);
36 list.add(student);
37 student = new Student(2,"李四",new Date(),null);
38 list.add(student);
39 student = new Student(3,"王五",null,"山东");
40 list.add(student);
41 StudentAnalysis analysis = new StudentAnalysis(list);
42 json = JSON.toJSONString(analysis,SerializerFeature.WriteMapNullValue);
43 System.out.println(json);
44 }
45
46 @Test
47 public void jsonToObject(){
48 String json = "{\"address\":null,\"birth\":1486015195815,\"createDate\":1486015195815,\"id\":10,\"name\":\"张三\"}";
49 Student student = JSON.parseObject(json,Student.class);
50 System.out.println(student);
51
52 json = "{\"students\":[{\"address\":null,\"birth\":1486015524472,\"createDate\":1486015524472,\"id\":1,\"name\":\"张三\"},{\"address\":null,\"birth\":1486015524472,\"createDate\":null,\"id\":2,\"name\":\"李四\"},{\"address\":\"山东\",\"birth\":null,\"createDate\":1486015524472,\"id\":3,\"name\":\"王五\"}]}";
53 StudentAnalysis analysis = JSON.parseObject(json,StudentAnalysis.class);
54 System.out.println(analysis);
55 }
56
57 @Test
58 public void ListToJson(){
59 List<Student> list = new ArrayList<Student>();
60 Student student = new Student(1,"张三",new Date(),null);
61 list.add(student);
62 student = new Student(2,"李四",new Date(),null);
63 list.add(student);
64 student = new Student(3,"王五",null,"山东");
65 list.add(student);
66
67 // list转化为 json 数组
68 String json = JSON.toJSONString(list,SerializerFeature.WriteMapNullValue);
69 System.out.println(json);
70
71 // json数组转为 list
72 List<Student> list1 = JSON.parseArray(json, Student.class);
73 System.out.println(list1);
74 }
75
76
77 @Test
78 public void MapToJson(){
79 Map<String,List<Student>> map = new HashMap<String,List<Student>>();
80 List<Student> list1 = new ArrayList<Student>();
81 Student student = new Student(1,"张三",new Date(),null);
82 list1.add(student);
83 student = new Student(2,"李四",new Date(),null);
84 list1.add(student);
85 student = new Student(3,"王五",null,"山东");
86 list1.add(student);
87
88 List<Student> list2 = new ArrayList<Student>();
89 student = new Student(4,"张三2",new Date(),null);
90 list2.add(student);
91 student = new Student(5,"李四2",new Date(),null);
92 list2.add(student);
93 student = new Student(6,"王五2",null,"山东");
94 list2.add(student);
95
96 List<Student> list3 = new ArrayList<Student>();
97 student = new Student(7,"张三2",new Date(),null);
98 list3.add(student);
99 student = new Student(8,"李四2",new Date(),null);
100 list3.add(student);
101 student = new Student(9,"王五2",null,"山东");
102 list3.add(student);
103
104 map.put("list1", list1);
105 map.put("list2", list2);
106 map.put("list3", list3);
107
108 // map 转为 json
109 String json = JSON.toJSONString(map,SerializerFeature.WriteMapNullValue);
110 System.out.println(json);
111
112 // json转为map
113 map = JSON.parseObject(json,new TypeReference<Map<String,List<Student>>>(){} );
114 System.out.println(map);
115 }
116 }

View Code

(2)工具类(在项目中可以直接使用)

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.fastjson.util;
2
3 import java.lang.reflect.Type;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7 import java.util.Map;
8
9 import com.alibaba.fastjson.JSON;
10 import com.alibaba.fastjson.TypeReference;
11 import com.alibaba.fastjson.serializer.SerializerFeature;
12 import com.model.Student;
13
14 /**
15 * @ClassName: FastJsonUtil
16 * @Description: TODO(FastJson工具类)
17 * @author xbq
18 * @date 2017-2-2 下午6:25:08
19 *
20 */
21 public class FastJsonUtil {
22
23 /**
24 * @Title: objectToJson
25 * @Description: TODO(实体类、List、Map转化为 json)
26 * @param @param obj
27 * @param @return 设定文件
28 * @return String 返回类型
29 * @throws
30 */
31 public static String objectToJson(Object obj){
32 // 缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出
33 return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
34 }
35
36 /**
37 * @Title: jsonToObject
38 * @Description: TODO(json转化为实体)
39 * @param @param json
40 * @param @param type
41 * @param @return 设定文件
42 * @return Object 返回类型
43 * @throws
44 */
45 public static Object jsonToObject(String json,Type type){
46 return JSON.parseObject(json, type);
47 }
48
49 /**
50 * @Title: jsonToList
51 * @Description: TODO(json转化为List)
52 * @param @param json
53 * @param @param obj 若传 Student的时候,则 传参数 new Student()
54 * @param @return 设定文件
55 * @return Object 返回类型
56 * @throws
57 */
58 @SuppressWarnings("rawtypes")
59 public static List jsonToList(String json,Object obj){
60 return JSON.parseArray(json, obj.getClass());
61 }
62
63 /**
64 * @Title: jsonToMap
65 * @Description: TODO(json转化为Map)
66 * @param @param json
67 * @param @param type
68 * @param @return 设定文件
69 * @return Object 返回类型
70 * @throws
71 */
72 @SuppressWarnings("rawtypes")
73 public static Map jsonToMap(String json,Type type){
74 return JSON.parseObject(json,new TypeReference<Map<String,List>>(){});
75 }
76
77 // 测试
78 public static void main(String[] args) {
79
80 List<Student> list = new ArrayList<Student>();
81 Student student = new Student(1,"张三",new Date(),null);
82 list.add(student);
83 student = new Student(2,"李四",new Date(),null);
84 list.add(student);
85 student = new Student(3,"王五",null,"山东");
86 list.add(student);
87
88 // list转化为 json 数组
89 String json = objectToJson(list);
90 System.out.println(json);
91
92 // json数组转为 list
93 @SuppressWarnings("unchecked")
94 List<Student> list1 = jsonToList(json, new Student());
95 System.out.println(list1);
96 }
97 }

View Code

4.Json-lib测试类

(1)测试类

Gson、FastJson、json-lib对比与实例_json-libGson、FastJson、json-lib对比与实例_fastJson_02


1 package com.jsonlib.test;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7
8 import net.sf.json.JSONArray;
9 import net.sf.json.JSONObject;
10 import net.sf.json.JsonConfig;
11 import net.sf.json.processors.JsonValueProcessor;
12
13 import org.junit.Test;
14
15 import com.model.Student;
16
17 public class TestJson_lib {
18
19 // 对象转为json
20 @Test
21 public void ObjectToJson(){
22 Student student = new Student(10,"张三",new Date(),null);
23
24 JsonConfig config=new JsonConfig();
25 //设置 json转换的处理器用来处理日期类型
26 //凡是反序列化Date类型的对象,都会经过该处理器进行处理
27 config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
28 //参数1 :属性名参数2:json对象的值参数3:jsonConfig对象
29 public Object processObjectValue(String arg0, Object arg1,JsonConfig arg2) {
30 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
31 Date d = (Date) arg1;
32 return sdf.format(d);
33 }
34 public Object processArrayValue(Object arg0, JsonConfig arg1) {
35 return null;
36 }
37 });
38
39 JSONObject jsonObject = JSONObject.fromObject(student,config);
40 System.out.println(jsonObject.toString());
41 }
42
43 // json转为对象
44 @Test
45 public void JsonToObject(){
46 String json = "{\"address\":\"\",\"birth\":\"2017-02-02 16:14:42\",\"id\":10,\"name\":\"张三\"}";
47 JsonConfig config=new JsonConfig();
48 //设置 json转换的处理器用来处理日期类型
49 //凡是反序列化Date类型的对象,都会经过该处理器进行处理
50 config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {
51 //参数1 :属性名参数2:json对象的值参数3:jsonConfig对象
52 public Object processObjectValue(String arg0, Object arg1,JsonConfig arg2) {
53 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
54 Date d = (Date) arg1;
55 return sdf.format(d);
56 }
57 public Object processArrayValue(Object arg0, JsonConfig arg1) {
58 return null;
59 }
60 });
61 Student student = (Student) JSONObject.toBean(JSONObject.fromObject(json,config), Student.class);
62 System.out.println(student);
63 }
64
65 @SuppressWarnings({ "deprecation", "unchecked" })
66 @Test
67 public void ListToJson(){
68 List<Student> list = new ArrayList<Student>();
69 Student student = new Student(1,"张三",new Date(),null);
70 list.add(student);
71 student = new Student(2,"李四",new Date(),null);
72 list.add(student);
73 student = new Student(3,"王五",null,"山东");
74 list.add(student);
75
76 // list转json
77 JSONArray jsonArray = JSONArray.fromObject(list);
78 System.out.println(jsonArray);
79
80 // json转为list
81 list = JSONArray.toList(JSONArray.fromObject(jsonArray.toString()),Student.class);
82 System.out.println(list);
83
84 // 遍历list
85 for(Student s : list){
86 System.out.println(s);
87 }
88 }
89
90 }

View Code