Simple Usage of java FastJson
- 2021-11-13 01:30:06
- OfStack
1. Preface
1.1. Introduction to FastJson:
JSON
(javaScript Object Notation) is a lightweight data exchange format. Mainly uses key-value pairs (
{"name": "json"}
) to save and represent data.
JSON
Yes
JS
Object, which uses text to represent a
JS
Object, which is essentially a string.
There are many processors for JSON. Here I introduce FastJson. FastJson is Ali's open source JSON parsing library, which can parse JSON format strings, support serialization of Java Bean into JSON strings, and deserialize JSON strings. Is an excellent Json framework, Github address: FastJson
1.2. Features of FastJson:
1. FastJson is fast, and it is a well-deserved fast regardless of serialization and deserialization
2. Powerful (support for common JDK classes including any Java, Bean, Class, Collection, Map, Date, or enum)
3. Zero dependency (no dependency on any other class library)
1.3. Simple description of FastJson:
FastJson mainly uses the following three classes to parse json format strings:
1. JSON: fastJson parser for JSON format string and JSON object and javaBean conversion
2. JSONObject: json object provided by fastJson
3. JSONArray: fastJson provides json array objects
2. Usage of FastJson
First, define three strings in json format
//json String - Simple object type
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
//json String - Array type
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
// Complex format json String
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}]}";
2.1. Conversion between JSON format strings and JSON objects
2.1. 1. json String-Conversion between simple object type and JSONObject
/**
* json String - Simple object type to JSONObject Conversion of
*/
@Test
public void testJSONStrToJSONObject() {
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
System.out.println("studentName: " + jsonObject.getString("studentName") + ":" + " studentAge: "
+ jsonObject.getInteger("studentAge"));
}
/**
* JSONObject To json String - Conversion of simple object type
*/
@Test
public void testJSONObjectToJSONStr() {
// Known JSONObject, The target is to be converted to json String
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
// No. 1 1 A kind of way
String jsonString = JSONObject.toJSONString(jsonObject);
// No. 1 2 A kind of way
//String jsonString = jsonObject.toJSONString();
System.out.println(jsonString);
}
2.1. 3. Conversion between complex json format strings and JSONObject
/**
* Complex json Format string to JSONObject Conversion of
*/
@Test
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");
// Get JSONObject Data in
String courseName = jsonObjectcourse.getString("courseName");
Integer code = jsonObjectcourse.getInteger("code");
System.out.println("courseName: " + courseName + " code: " + code);
JSONArray jsonArraystudents = jsonObject.getJSONArray("students");
// Traversal JSONArray
for (Object object : jsonArraystudents) {
JSONObject jsonObjectone = (JSONObject) object;
String studentName = jsonObjectone.getString("studentName");
Integer studentAge = jsonObjectone.getInteger("studentAge");
System.out.println("studentName: " + studentName + " studentAge: " + studentAge);
}
}
/**
* Complex JSONObject To json Conversion of format string
*/
@Test
public void testJSONObjectToComplexJSONStr() {
// Complex JSONObject, The target is to be converted to json String
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
// No. 1 1 A kind of way
//String jsonString = JSONObject.toJSONString(jsonObject);
// No. 1 2 A kind of way
String jsonString = jsonObject.toJSONString();
System.out.println(jsonString);
}
2.2. Conversion between JSON format string and javaBean
2.2. 1. json String-Conversion between simple object type and javaBean
/**
* json String - Simple object to JavaBean Conversion between
*/
@Test
public void testJSONStrToJavaBeanObj() {
// No. 1 1 A kind of way
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
String studentName = jsonObject.getString("studentName");
Integer studentAge = jsonObject.getInteger("studentAge");
//Student student = new Student(studentName, studentAge);
// No. 1 2 A kind of way , Use TypeReference<T> Class , Because its construction method uses protected To embellish , Therefore, its subclasses are created
//Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
// No. 1 3 A kind of way , Use Gson Thoughts of
Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
System.out.println(student);
}
/**
* JavaBean To json String - Conversion of simple objects
*/
@Test
public void testJavaBeanObjToJSONStr() {
Student student = new Student("lily", 12);
String jsonString = JSONObject.toJSONString(student);
System.out.println(jsonString);
}
2.3. Conversion between javaBean and json objects
2.3. 1. Conversion between simple javaBean and json objects
/**
* Simple JavaBean_obj To json Conversion of objects
*/
@Test
public void testJavaBeanToJSONObject(){
// Known simple JavaBean_obj
Student student = new Student("lily", 12);
// Mode 1
String jsonString = JSONObject.toJSONString(student);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
// Mode 2
JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student);
System.out.println(jsonObject1);
}
/**
* Simple json Object to JavaBean_obj Conversion of
*/
@Test
public void testJSONObjectToJavaBean(){
// Known simple json Object
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
// No. 1 1 A kind of way , Use TypeReference<T> Class , Because its construction method uses protected To embellish , Therefore, its subclasses are created
Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {});
System.out.println(student);
// No. 1 2 A kind of way , Use Gson Thoughts of
Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
System.out.println(student1);
}
2.3. 2. Conversion between JavaList and JsonArray
/**
* JavaList To JsonArray Conversion of
*/
@Test
public void testJavaListToJsonArray() {
// Known 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);
// Mode 1
String jsonString = JSONArray.toJSONString(students);
JSONArray jsonArray = JSONArray.parseArray(jsonString);
System.out.println(jsonArray);
// Mode 2
JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students);
System.out.println(jsonArray1);
}
/**
* JsonArray To JavaList Conversion of
*/
@Test
public void testJsonArrayToJavaList() {
// Known JsonArray
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
// No. 1 1 A kind of way , Use TypeReference<T> Class , Because its construction method uses protected To embellish , Therefore, its subclasses are created
ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(),
new TypeReference<ArrayList<Student>>() {});
System.out.println(students);
// No. 1 2 A kind of way , Use Gson Thoughts of
List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);
System.out.println(students1);
}
2.3. 3. Conversion between complex JavaBean_obj and json objects
/**
* Complex JavaBean_obj To json Conversion of objects
*/
@Test
public void testComplexJavaBeanToJSONObject() {
// Known complexity 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);
// Mode 1
String jsonString = JSONObject.toJSONString(teacher);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
// Mode 2
JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(teacher);
System.out.println(jsonObject1);
}
/**
* Complex json Object to JavaBean_obj Conversion of
*/
@Test
public void testComplexJSONObjectToJavaBean() {
// Known complexity json Object
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
// No. 1 1 A kind of way , Use TypeReference<T> Class , Because its construction method uses protected To embellish , Therefore, its subclasses are created
Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Teacher>() {});
System.out.println(teacher);
// No. 1 2 A kind of way , Use Gson Thoughts of
Teacher teacher1 = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class);
System.out.println(teacher1);
}
Summarize
// Put JSON Text parse For JSONObject Or JSONArray
public static final Object parse(String text);
// Put JSON Text parse Cheng JSONObject
public static final JSONObject parseObject(String text);
// Put JSON Text parse For JavaBean
public static final <T> T parseObject(String text, Class<T> clazz);
// Put JSON Text parse Cheng JSONArray
public static final JSONArray parseArray(String text);
// Put JSON Text parse Cheng JavaBean Set
public static final <T> List<T> parseArray(String text, Class<T> clazz);
// Will JavaBean Serialize JSON Text
public static final String toJSONString(Object object);
// Will JavaBean Serialize to formatted JSON Text
public static final String toJSONString(Object object, boolean prettyFormat);
// Will JavaBean Convert to JSONObject Or JSONArray .
public static final Object toJSON(Object javaObject);
Reference
https://segmentfault.com/a/1190000011212806
https://www.cnblogs.com/miracle-luna/p/11143702.html
https://github.com/alibaba/fastjson