Fastjson is responsible for the conversion between JSON format strings, JSON objects and JavaBeans
Fastjason mainly uses the following three classes for parsing JSON format strings:
JSON: the parser of fastjson, which is used to convert JSON format strings to JSON objects and JavaBeans.
Jsonobject: the JSON object provided by fastjson.
Jsonarray: fastjson provides JSON array objects.
We can look at jsonobject as a map < string, Object >, but jsonobject provides more rich and convenient methods to facilitate our operation on object properties. Let's take a look at the source code.
Similarly, we can regard jsonarray as a list < Object >, and jsonarray as a collection of jsonobject objects.
In addition, because jsonobject and jsonarray inherit JSON, they can also be directly used to convert JSON format strings to JSON objects and JavaBeans. However, to avoid confusion, we still use JSON.
First, we define three strings in JSON format as our data source.
Example 1: conversion between JSON format string and JSON object.
Example 1.1-json string - conversion between simple object and jsonobject
Example 1.2-json string - conversion between array type and jsonarray
Example 1.3 - conversion between complex JSON format string and jsonobject
Example 2: conversion between JSON format string and JavaBean.
First, we provide three JavaBeans for the string shown in the data source.
Typereference < T > is recommended for the conversion between JSON string and JavaBean. Using generics can be clearer. Of course, there are other conversion methods, which will not be discussed here.
Example 2.1 - JSON string - conversion between simple object type and JavaBean
Example 2.2-json string - conversion between array type and JavaBean
Example 2.3 - conversion between complex JSON format string and JavaBean
For typereference < T >, because its construction method is decorated with protected, when creating its objects under other packages, it is necessary to use the subclass of its implementation class: new typereference < teacher > () {}
In addition:
1. For the conversion between JSON object and JSON format string, you can directly use the tojsonstring () method.
2. The conversion between JavaBean and JSON format string requires JSON toJSONString(obj);
3. Conversion between JavaBean and JSON object: JSON To JSON (obj), and then use cast, jsonobject or jsonarray.
Finally, as programmers, we should study the problem carefully and deeply. When you have a thorough understanding of the principle, it will be easy to develop, many problems and doubts in development will be solved, and you can bypass other problems by analogy. Of course, there is not much time for you to study the principle in the development. The development should be based on the realization of functions. After the project is launched, you have a lot of time or spare time. You can go to the bottom and deeply study a technology. In order to feel that it is very important for the growth of a programmer.
summary
The above is what Xiaobian introduced to you. Fastjason hopes to help you with the conversion between JSON format strings, JSON objects and JavaBeans. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!