本文主要描述FastJson的使用,FastJson是阿里巴巴的的开源库,用于对JSON格式的数据进行解析和打包。顾名思义,FastJson操作JSON的特点就是速度非常快的,业界一般都会使用阿里的FastJson。
1.添加依赖
在Maven项目中使用FastJson库,只需要添加一个依赖包,依赖包如下:1
2
3
4
5<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
如果想使用其他版本,可点击该链接MAVEN仓库,搜索fastjson,进行依赖坐标的查找。
2.FastJson的使用
FastJson的转换形式有如下几个:
1.JSON格式字符串与JSON对象之间的转换
1.1json字符串-简单对象型与JSONObject之间的转换
1.2json字符串(数组类型)与JSONArray之间的转换
1.3复杂json格式字符串与JSONObject之间的转换
2.JSON格式字符串与JavaBean之间的转换
2.1json字符串-简单对象型与JavaBean之间的转换
2.2json字符串-数组类型与JavaBean之间的转换
2.3复杂json格式字符串与JavaBean之间的转换
3.JavaBean与JSON对象之间的转换
3.1简单JavaBean与JSON对象之间的转换
3.2JavaList与JSONArray之间的转换
3.3复杂JavaBean_obj与json对象之间的转换
Student对象:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package com.cnlive.json;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String studentName;
private Integer studentAge;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String studentName, Integer studentAge) {
super();
this.studentName = studentName;
this.studentAge = studentAge;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
public String toString() {
return "Student [studentName=" + studentName + ", studentAge="
+ studentAge + "]";
}
}
Teacher对象:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66package com.cnlive.json;
import java.io.Serializable;
import java.util.List;
public class Teacher implements Serializable {
private static final long serialVersionUID = 1L;
private String teacherName;
private Integer teacherAge;
private Course course;
private List<Student> students;
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(String teacherName, Integer teacherAge, Course course,
List<Student> students) {
super();
this.teacherName = teacherName;
this.teacherAge = teacherAge;
this.course = course;
this.students = students;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Integer getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(Integer teacherAge) {
this.teacherAge = teacherAge;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public String toString() {
return "Teacher [teacherName=" + teacherName + ", teacherAge="
+ teacherAge + ", course=" + course + ", students=" + students
+ "]";
}
}
Course对象:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42package com.cnlive.json;
import java.io.Serializable;
public class Course implements Serializable {
private static final long serialVersionUID = 1L;
private String courseName;
private Integer code;
public Course() {
// TODO Auto-generated constructor stub
}
public Course(String courseName, Integer code) {
super();
this.courseName = courseName;
this.code = code;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String toString() {
return "Course [courseName=" + courseName + ", code=" + code + "]";
}
}
JSON测试类:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368package com.cnlive.json;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
public class FastJson {
// json字符串-简单对象型
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
// json字符串-数组类型
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
// 复杂格式json字符串
private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,
\"course\":{\"courseName\":\"english\",\"code\":1270},
\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
复杂格式json字符串解析后:
{
"teacherName":"crystall",
"teacherAge":27,
"course":{
"courseName":"english",
"code":1270
},
"students":[
{
"studentName":"lily",
"studentAge":12
},
{
"studentName":"lucy",
"studentAge":15
}
]
}
/**
* 1.JSON格式字符串与JSON对象之间的转换
*/
// 1.1json字符串-简单对象型与JSONObject之间的转换
// 1.1.1json字符串-简单对象型到JSONObject的转换
public void testJSONStrToJSONObject() {
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
System.out.println("studentName: "
+ jsonObject.getString("studentName") + "," + " studentAge: "
+ jsonObject.getInteger("studentAge"));
}
// 1.1.2JSONObject到json字符串-简单对象型的转换
public void testJSONObjectToJSONStr() {
// 已知JSONObject,目标要转换为json字符串
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
// 第一种方式
// String jsonString = JSONObject.toJSONString(jsonObject);
// 第二种方式
String jsonString = jsonObject.toJSONString();
System.out.println(jsonString);
}
// 1.2json字符串(数组类型)与JSONArray之间的转换
// 1.2.1json字符串-数组类型到JSONArray的转换
public void testJSONStrToJSONArray() {
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
// 遍历方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println("studentName: "
+ jsonObject.getString("studentName") + ","
+ "studentAge: " + jsonObject.getInteger("studentAge"));
}
// 遍历方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println("studentName: "
+ jsonObject.getString("studentName") + ","
+ "studentAge: " + jsonObject.getInteger("studentAge"));
}
}
// 1.2.2JSONArray到json字符串-数组类型的转换
public void testJSONArrayToJSONStr() {
// 已知JSONArray,目标要转换为json字符串
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
// 第一种方式
String jsonString = JSONArray.toJSONString(jsonArray);
System.out.println(jsonString);
// 第二种方式
String jsonString2 = jsonArray.toJSONString();
System.out.println(jsonString2);
}
// 1.3复杂json格式字符串与JSONObject之间的转换
// 1.3.1复杂json格式字符串到JSONObject的转换
public void testComplexJSONStrToJSONObject() {
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
System.out.println("teacherName: " + teacherName + " , "
+ "teacherAge: " + teacherAge);
JSONObject jsonObjectcourse = jsonObject.getJSONObject("course");
String courseName = jsonObjectcourse.getString("courseName");
Integer code = jsonObjectcourse.getInteger("code");
System.out.println("courseName: " + courseName + " , " + "code: "
+ code);
JSONArray jsonArray = jsonObject.getJSONArray("students");
// 遍历JSONArray
for (Object obj : jsonArray) {
JSONObject jsonObjectone = (JSONObject) obj;
String studentName = jsonObjectone.getString("studentName");
Integer studentAge = jsonObjectone.getInteger("studentAge");
System.out.println("studentName: " + studentName + " , "
+ "studentAge: " + studentAge);
}
}
// 1.3.2复杂JSONObject到json格式字符串的转换
public void testJSONObjectToComplexJSONStr() {
// 已知复杂JSONObject,目标要转换为json字符串
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
// 第一种方式
String jsonString = jsonObject.toJSONString();
System.out.println(jsonString);
// 第二种方式
String jsonString2 = JSONObject.toJSONString(jsonObject);
System.out.println(jsonString2);
}
/**
* 2.JSON格式字符串与JavaBean之间的转换
*/
// 2.1json字符串-简单对象型与JavaBean之间的转换
// 2.1.1json字符串-简单对象型到JavaBean的转换
public void testJSONStrToJavaBeanObj() {
// 第一种方式
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
String studentName = jsonObject.getString("studentName");
Integer studentAge = jsonObject.getInteger("studentAge");
System.out.println("studentName: " + studentName + " , "
+ "studentAge:" + studentAge);
// 第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
Student student = JSONObject.parseObject(JSON_OBJ_STR,
new TypeReference<Student>() {
});
System.out.println("studentName: " + student.getStudentName() + " , "
+ "studentAge:" + student.getStudentAge());
// 第三种方式,使用Gson的思想
Student student2 = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
System.out.println("studentName: " + student2.getStudentName() + " , "
+ "studentAge:" + student2.getStudentAge());
}
// 2.1.2JavaBean到json字符串-简单对象的转换
public void testJavaBeanObjToJSONStr() {
Student student = new Student("lily", 12);
String jsonString = JSONObject.toJSONString(student);
System.out.println(jsonString);
}
// 2.2json字符串-数组类型与JavaBean之间的转换
// 2.2.1json字符串-数组类型到JavaBean_List的转换
public void testJSONStrToJavaBeanList() {
// 第一种方式
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
// 遍历JSONArray
List<Student> students = new ArrayList<Student>();
Student student = null;
for (Object obj : jsonArray) {
JSONObject jsonObjectone = (JSONObject) obj;
String studentName = jsonObjectone.getString("studentName");
Integer studentAge = jsonObjectone.getInteger("studentAge");
student = new Student(studentName, studentAge);
students.add(student);
}
System.out.println("students: " + students + " , " + "size: "
+ students.size());
// 第二种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
List<Student> studentList = JSONArray.parseObject(JSON_ARRAY_STR,
new TypeReference<ArrayList<Student>>() {
});
System.out.println("studentList: " + studentList);
// 第三种方式,使用Gson的思想
List<Student> studentList2 = JSONArray.parseArray(JSON_ARRAY_STR,
Student.class);
System.out.println("studentList2: " + studentList2);
}
// 2.2.2JavaBean_List到json字符串-数组类型的转换
public void testJavaBeanListToJSONStr() {
Student student = new Student("lily", 12);
Student studenttwo = new Student("lucy", 15);
List<Student> students = new ArrayList<Student>();
students.add(student);
students.add(studenttwo);
String jsonString = JSONArray.toJSONString(students);
System.out.println(jsonString);
}
// 2.3复杂json格式字符串与JavaBean之间的转换
// 2.3.1复杂json格式字符串到JavaBean_obj的转换
public void textComplexJSONStrToJavaBean() {
// 第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,
new TypeReference<Teacher>() {
});
System.out.println(teacher);
// 第二种方式,使用Gson思想
Teacher teacher2 = JSONObject.parseObject(COMPLEX_JSON_STR,
Teacher.class);
System.out.println(teacher2);
}
// 2.3.2复杂JavaBean_obj到json格式字符串的转换
public void testJavaBeanToComplexJSONStr() {
// 已知复杂JavaBean_obj
Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,
new TypeReference<Teacher>() {
});
String jsonString = JSONObject.toJSONString(teacher);
System.out.println(jsonString);
}
/**
* 3.JavaBean与JSON对象之间的转换
*/
//3.1简单JavaBean与JSON对象之间的转换
//3.1.1简单JavaBean_obj到JSON对象的转换
public void testJavaBeanToJSONObject(){
//已知简单JavaBean_obj
Student student = new Student("lily",12);
//方式一
String jsonString = JSONObject.toJSONString(student);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//方式二
JSONObject jsonObject2 = (JSONObject)JSONObject.toJSON(student);
System.out.println(jsonObject2);
}
//3.1.2简单json对象到JavaBean_obj的转换
public void testJSONObjectToJavaBean(){
//已知简单json对象
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
Student student = JSONObject.parseObject(jsonObject.toJSONString(),new TypeReference<Student>(){});
System.out.println(student);
//第二种方式,使用Gson的思想
Student student2 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
System.out.println(student2);
}
//3.2JavaList与JSONArray之间的转换
//3.2.1JavaList到JsonArray的转换
public void testJavaListToJSONArray(){
//已知JavaList
Student student = new Student("lily", 12);
Student studenttwo = new Student("lucy", 15);
List<Student> students = new ArrayList<Student>();
students.add(student);
students.add(studenttwo);
//方式一
String jsonString = JSONArray.toJSONString(students);
JSONArray jsonArray = JSONArray.parseArray(jsonString);
System.out.println(jsonArray);
//方式二
JSONArray jsonArray2 = (JSONArray)JSONArray.toJSON(students);
System.out.println(jsonArray2);
}
//3.2.2JsonArray到JavaList的转换
public void testJsonArrayToJavaList() {
//已知JsonArray
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(),
new TypeReference<ArrayList<Student>>() {});
System.out.println(students);
//第二种方式,使用Gson的思想
List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);
System.out.println(students1);
}
//3.3复杂JavaBean_obj与json对象之间的转换
//3.3.1复杂JavaBean_obj到json对象的转换
public void testComplexJavaBeanToJSONObject() {
//已知复杂JavaBean_obj
Student student = new Student("lily", 12);
Student studenttwo = new Student("lucy", 15);
List<Student> students = new ArrayList<Student>();
students.add(student);
students.add(studenttwo);
Course course = new Course("english", 1270);
Teacher teacher = new Teacher("crystall", 27, course, students);
//方式一
String jsonString = JSONObject.toJSONString(teacher);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//方式二
JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(teacher);
System.out.println(jsonObject1);
}
//3.3.2复杂json对象到JavaBean_obj的转换
public void testComplexJSONObjectToJavaBean() {
//已知复杂json对象
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
//第一种方式,使用TypeReference<T>类,由于其构造方法使用protected进行修饰,故创建其子类
Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Teacher>() {});
System.out.println(teacher);
//第二种方式,使用Gson的思想
Teacher teacher1 = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class);
System.out.println(teacher1);
}
}
2.FastJson的补充
Map与JSON对象互转1
2
3
4
5
6
7
8
9
10
11
12
13//Map转JSON对象
Map<String, Object> map = new HashMap<>(2);
map.put("username", "xiaoming");
map.put("password", 123);
JSONObject json = new JSONObject(map);
System.out.println(json.toJSONString())
//JSON对象转Map
JSONObject json =new JSONObject();
json.put("username","xiaoming");
json.put("password",123);
Map<String, Object> map = json;
System.out.println(map);
Map与JSON字符串互转1
2
3
4
5
6
7
8
9
10
11//JSON字符串转Map
private static final String JSON_OBJ_STR ="{\"name\":\"xiaoming\",\"age\":12}";
Map<String, Object> map = JSONObject.parseObject(JSON_OBJ_STR);
System.out.println(map);
//Map转JSON字符串
Map<String, Object> map = new HashMap<>(2);
map.put("username", "xiaoming");
map.put("password", 123);
String str = JSONObject.toJSONString(map);
System.out.println(str);
List与JSON字符串互转1
2
3
4
5String aaa="[\"aaa\",\"bbb\",\"ccc\"]";
List<String> strings = JSONArray.parseArray(aaa, String.class);
System.out.println(strings);
String s = JSONArray.toJSONString(strings);
System.out.println(s);
List与JSON对象互转1
2
3
4
5
6
7
8
9List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
String s = JSONArray.toJSONString(list);
JSONArray jsonArray = JSONArray.parseArray(s);
System.out.println(jsonArray);
List<String> strings = jsonArray.toJavaList(String.class);
System.out.println(strings);