1. Preface
1.1. Introduction of FastJson:
JSON(javaScript Object Notation) is a lightweight data exchange format. The key value pair ({"name": "json"}) is used to save and represent data. JSON is a string representation of JS objects. It uses text to represent the information of a JS object, which is essentially a string.
There are many processors in JSON. Here I introduce FastJson, which is Ali's open source JSON parsing library. FastJson can parse JSON format strings, support serialization of Java beans into JSON strings, and can also be de-serialized from JSON strings to JavaBean s. It's an excellent Json framework, Github address: FastJson
1.2. The characteristics of FastJson:
1.FastJson is fast in number, deserving of both 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 libraries)
1.3. A brief description of FastJson:
FastJson's analysis of json format strings is mainly used in the following three classes:
1.JSON: fastJson parser for conversion of JSON format strings to JSON objects and Java beans
2.JSONObject: json object provided by fastJson
3.JSONArray: fastJson provides json array objects
2. Use of FastJson
First define three json formatted strings
//json Character string-Simple object type private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}"; //json Character string-Array type private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]"; //Complex format json Character 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 Conversion from String-Simple Object to JSONObject */ @Test public void testJSONStrToJSONObject() { JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); System.out.println("studentName: " + jsonObject.getString("studentName") + ":" + " studentAge: " + jsonObject.getInteger("studentAge")); } /** * JSONObject Conversion to json string - simple object type */ @Test public void testJSONObjectToJSONStr() { //Known JSONObject,Target to be converted to json Character string JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); // The first way String jsonString = JSONObject.toJSONString(jsonObject); // The second way //String jsonString = jsonObject.toJSONString(); System.out.println(jsonString); }
2.1.3. Conversion between complex json format strings and JSONObject
/** * Conversion of complex json format strings to JSONObject */ @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"); //Obtain 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"); //ergodic 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); } } /** * Conversion of complex JSONObject to json format strings */ @Test public void testJSONObjectToComplexJSONStr() { //complex JSONObject,Target to be converted to json Character string JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR); //The first way //String jsonString = JSONObject.toJSONString(jsonObject); //The second way String jsonString = jsonObject.toJSONString(); System.out.println(jsonString); }
2.2. Conversion between JSON format strings and javaBean s
2.2.1.json String - Conversion between Simple Object Type and javaBean
/** * json String-Simple Object to JavaBean Conversion */ @Test public void testJSONStrToJavaBeanObj() { //The first way JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); String studentName = jsonObject.getString("studentName"); Integer studentAge = jsonObject.getInteger("studentAge"); //Student student = new Student(studentName, studentAge); //The second way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses //Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); //The third way,Use Gson Thoughts Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class); System.out.println(student); } /** * JavaBean Conversion to json string - Simple Object */ @Test public void testJavaBeanObjToJSONStr() { Student student = new Student("lily", 12); String jsonString = JSONObject.toJSONString(student); System.out.println(jsonString); }
2.2.2. Conversion between string-array type and javaBean
/** * json Conversion of string-array type to JavaBean_List */ @Test public void testJSONStrToJavaBeanList() { //The first way JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR); //ergodic JSONArray List<Student> students = new ArrayList<Student>(); Student student = null; for (Object object : jsonArray) { JSONObject jsonObjectone = (JSONObject) object; String studentName = jsonObjectone.getString("studentName"); Integer studentAge = jsonObjectone.getInteger("studentAge"); student = new Student(studentName,studentAge); students.add(student); } System.out.println("students: " + students); //The second way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses List<Student> studentList = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {}); System.out.println("studentList: " + studentList); //The third way,Use Gson Thoughts List<Student> studentList1 = JSONArray.parseArray(JSON_ARRAY_STR, Student.class); System.out.println("studentList1: " + studentList1); } /** * JavaBean_List Conversion to json string-array type */ @Test 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.2.3. Conversion between complex json format strings and javaBean s
/** * Conversion of complex json format strings to JavaBean_obj */ @Test public void testComplexJSONStrToJavaBean(){ //The first way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {}); System.out.println(teacher); //The second way,Use Gson thought Teacher teacher1 = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class); System.out.println(teacher1); } /** * Conversion of complex JavaBean_obj to json format strings */ @Test public void testJavaBeanToComplexJSONStr(){ //Known complexity JavaBean_obj Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {}); String jsonString = JSONObject.toJSONString(teacher); System.out.println(jsonString); }
2.3. Conversion between javaBean and json objects
2.3.1. Conversion between simple javaBean s and json objects
/** * Conversion from Simple JavaBean_obj to json Object */ @Test public void testJavaBeanToJSONObject(){ //Known Simplicity JavaBean_obj Student student = new Student("lily", 12); //Mode I 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); } /** * Conversion from Simple json Object to JavaBean_obj */ @Test public void testJSONObjectToJavaBean(){ //Known Simplicity json object JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR); //The first way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Student>() {}); System.out.println(student); //The second way,Use Gson Thoughts Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class); System.out.println(student1); }
2.3.2. Conversion between JavaList and Json Array
/** * JavaList Conversion to Json Array */ @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 I 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 Conversion to JavaList */ @Test public void testJsonArrayToJavaList() { //Known JsonArray JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR); //The first way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(), new TypeReference<ArrayList<Student>>() {}); System.out.println(students); //The second way,Use Gson Thoughts List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class); System.out.println(students1); }
2.3.2. Conversion between JavaList and Json Array
/** * JavaList Conversion to Json Array */ @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 I 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 Conversion to JavaList */ @Test public void testJsonArrayToJavaList() { //Known JsonArray JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR); //The first way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses ArrayList<Student> students = JSONArray.parseObject(jsonArray.toJSONString(), new TypeReference<ArrayList<Student>>() {}); System.out.println(students); //The second way,Use Gson Thoughts List<Student> students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class); System.out.println(students1); }
2.3.3. Conversion between complex JavaBean_obj and json objects
/** * Conversion of complex JavaBean_obj to json 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 I 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); } /** * Conversion of complex json objects to JavaBean_obj */ @Test public void testComplexJSONObjectToJavaBean() { //Known complexity json object JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR); //The first way,Use TypeReference<T>class,Because of the use of its construction method protected Modify,So create its subclasses Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Teacher>() {}); System.out.println(teacher); //The second way,Use Gson Thoughts Teacher teacher1 = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class); System.out.println(teacher1); }
summary
// hold JSON text parse by JSONObject perhaps JSONArray public static final Object parse(String text); // hold JSON text parse become JSONObject public static final JSONObject parseObject(String text); // hold JSON text parse by JavaBean public static final <T> T parseObject(String text, Class<T> clazz); // hold JSON text parse become JSONArray public static final JSONArray parseArray(String text); //hold JSON text parse become JavaBean aggregate public static final <T> List<T> parseArray(String text, Class<T> clazz); // take JavaBean Serialization JSON text public static final String toJSONString(Object object); // take JavaBean Serialized to formatted JSON text public static final String toJSONString(Object object, boolean prettyFormat); //take JavaBean Convert to JSONObject perhaps JSONArray. public static final Object toJSON(Object javaObject);
Reference resources: