In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the characteristics and usage of FastJson in java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the characteristics and usage of FastJson in java".
Catalogue
1. Preface
Introduction to 1.1.FastJson:
Characteristics of 1.2.FastJson:
A simple description of 1.3.FastJson:
The usage of 2.FastJson
Conversion between 2.1.JSON format string and JSON object
Conversion between 2.2.JSON format string and javaBean
Conversion between 2.3.javaBean and json objects
Summary
1. Introduction to 1.1.FastJson:
JSON (javaScript Object Notation) is a lightweight data exchange format. Data is mainly saved and represented by key-value pairs ({"name": "json"}). JSON is a string representation of a JS object, which uses text to represent the information of a JS object, which is essentially a string.
There are many processors for JSON. Here I introduce that FastJson,FastJson is Ali's open source JSON parsing library, which can parse strings in JSON format, support serialization of JavaBean into JSON strings, and deserialize from JSON strings to JavaBean. Is an excellent Json framework, Github address: FastJson
Characteristics of 1.2.FastJson:
1.FastJson is fast, regardless of serialization and deserialization, it is a well-deserved fast.
two。 Powerful (supports normal JDK classes including any Java Bean Class, Collection, Map, Date or enum)
3. Zero dependency (does not depend on any other class libraries)
A simple description of 1.3.FastJson:
FastJson mainly uses the following three classes for parsing json format strings:
Parser for 1.JSON:fastJson, for conversion between JSON format strings and JSON objects and javaBean
Json object provided by 2.JSONObject:fastJson
3.JSONArray:fastJson provides json array objects
The usage of 2.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}]}" Conversion between 2.1.JSON format string and JSON object
2.1.1.json string-conversion between simple object type and JSONObject
/ * json string-simple object to JSONObject conversion * / @ Testpublic void testJSONStrToJSONObject () {JSONObject jsonObject = JSONObject.parseObject (JSON_OBJ_STR); System.out.println ("studentName:" + jsonObject.getString ("studentName") + ":" + "studentAge:" + jsonObject.getInteger ("studentAge")) } / * JSONObject to json string-simple object type conversion * / @ Testpublic void testJSONObjectToJSONStr () {/ / known JSONObject, the target is converted to the json string JSONObject jsonObject = JSONObject.parseObject (JSON_OBJ_STR); / / the first method String jsonString = JSONObject.toJSONString (jsonObject); / / the second method / / String jsonString = jsonObject.toJSONString (); System.out.println (jsonString);}
2.1.3. Conversion between complex json format string and JSONObject
/ * * conversion from complex json format string to JSONObject * / @ Testpublic 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 data in JSONObject String courseName = jsonObjectcourse.getString ("courseName"); Integer code = jsonObjectcourse.getInteger ("code"); System.out.println ("courseName:" + courseName + "code:" + code); JSONArray jsonArraystudents = jsonObject.getJSONArray ("students"); / / traverse 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 format string conversion * / @ Testpublic void testJSONObjectToComplexJSONStr () {/ / complex JSONObject, the target is to convert to json string JSONObject jsonObject = JSONObject.parseObject (COMPLEX_JSON_STR) / / the first method / / String jsonString = JSONObject.toJSONString (jsonObject); / / the second method String jsonString = jsonObject.toJSONString (); System.out.println (jsonString);} conversion between 2.2.JSON format string and javaBean
2.2.1.json string-conversion between simple object type and javaBean
/ * json string-conversion between simple objects to JavaBean * / @ Testpublic 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 is to use the TypeReference class, and because its constructor is decorated with protected, create its subclass / / Student student = JSONObject.parseObject (JSON_OBJ_STR, new TypeReference () {}); / / the third way, use the idea of Gson Student student = JSONObject.parseObject (JSON_OBJ_STR, Student.class); System.out.println (student) } / * JavaBean to json string-conversion of simple objects * / @ Testpublic void testJavaBeanObjToJSONStr () {Student student = new Student ("lily", 12); String jsonString = JSONObject.toJSONString (student); System.out.println (jsonString); conversion between 2.3.javaBean and json objects
2.3.1. Conversion between simple javaBean and json objects
/ * conversion from simple JavaBean_obj to json objects * / @ Testpublic void testJavaBeanToJSONObject () {/ / known simple JavaBean_obj Student student = new Student ("lily", 12); / / Mode-String jsonString = JSONObject.toJSONString (student); JSONObject jsonObject = JSONObject.parseObject (jsonString); System.out.println (jsonObject); / / Mode II JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON (student); System.out.println (jsonObject1) } / * simple json object to JavaBean_obj conversion * / @ Testpublic void testJSONObjectToJavaBean () {/ / known simple json object JSONObject jsonObject = JSONObject.parseObject (JSON_OBJ_STR); / / the first way is to use the TypeReference class, whose constructor is decorated with protected, so create its subclass Student student = JSONObject.parseObject (jsonObject.toJSONString (), new TypeReference () {}); System.out.println (student) / / the second way, use the idea of Gson Student student1 = JSONObject.parseObject (jsonObject.toJSONString (), Student.class); System.out.println (student1);}
Conversion between 2.3.2.JavaList and JsonArray
/ * conversion from JavaList to JsonArray * / @ Testpublic void testJavaListToJsonArray () {/ / known JavaList Student student = new Student ("lily", 12); Student studenttwo = new Student ("lucy", 15); List students = new ArrayList (); students.add (student); students.add (studenttwo); / / Mode-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);} / * conversion from JsonArray to JavaList * / @ Testpublic void testJsonArrayToJavaList () {/ / known JsonArray JSONArray jsonArray = JSONArray.parseArray (JSON_ARRAY_STR) / / the first way is to use the TypeReference class, and because its constructor is decorated with protected, create its subclass ArrayList students = JSONArray.parseObject (jsonArray.toJSONString (), new TypeReference () {}); System.out.println (students); / / the second way, use the idea of Gson List students1 = JSONArray.parseArray (jsonArray.toJSONString (), Student.class); System.out.println (students1);}
2.3.3. Conversion between complex JavaBean_obj and json objects
/ * conversion from complex JavaBean_obj to json objects * / @ Testpublic void testComplexJavaBeanToJSONObject () {/ / known complex JavaBean_obj Student student = new Student ("lily", 12); Student studenttwo = new Student ("lucy", 15); List students = new ArrayList (); students.add (student); students.add (studenttwo); Course course = new Course ("english", 1270); Teacher teacher = new Teacher ("crystall", 27, course, students) / / method 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);} / * * conversion from complex json objects to JavaBean_obj * / @ Testpublic void testComplexJSONObjectToJavaBean () {/ / known complex json objects JSONObject jsonObject = JSONObject.parseObject (COMPLEX_JSON_STR) / / the first way is to use the TypeReference class, and because its constructor is decorated with protected, create its subclass Teacher teacher = JSONObject.parseObject (jsonObject.toJSONString (), new TypeReference () {}); System.out.println (teacher); / / the second way, using the idea of Gson Teacher teacher1 = JSONObject.parseObject (jsonObject.toJSONString (), Teacher.class); System.out.println (teacher1) } summarize / / parse JSON text to JSONObject or JSONArray public static final Object parse (String text); / / parse JSON text to JSONObject public static final JSONObject parseObject (String text); / / parse JSON text to JavaBean public static final T parseObject (String text, Class clazz); / / parse JSON text to JSONArray public static final JSONArray parseArray (String text) / parse JSON text into JavaBean collection public static final List parseArray (String text, Class clazz); / / serialize JavaBean into JSON text public static final String toJSONString (Object object); / / serialize JavaBean into formatted JSON text public static final String toJSONString (Object object, boolean prettyFormat) / / convert JavaBean to JSONObject or JSONArray. Public static final Object toJSON (Object javaObject); at this point, I believe you have a deeper understanding of "the characteristics and usage of FastJson in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.