Explain the simple use of Android native JSON and fastjson
Android native operation JSON data
There are mainly two classes: jsonobject operation object and jonsarray operation JSON array
Object to JSON
Log log display
JSON to object
Create a new jsonobject, assign the JSON string through the construction method, the jsonobject object will have the JSON value, and then create an object to assign the value one by one. Jsonobject has different get methods for different types of values
List to JSON
Using jonsarray
JSON to list
Note: exceptions need to be caught in the process of using jsonobject and jsonarray
Do you feel very troublesome? If there are too many data, it's exhausting
The way to make it easier is to download fastjson.jar, which is known as the fastest JSON operation in history, and then use it
Fastjason operation data
Object to JSON
It's done in one sentence. It's so simple. Thank you Ma yunbaba!!!
JSON to object
Similarly, there is only one sentence, which is really touching compared with Android native
List to JSON
When three objects are added to the collection, the output of the JSON string becomes ref, {0} obviously, this is the first object to reference. Because you add the same object, fastjson does not create a direct reference, which is also the reason why it claims to be the fastest
However, there is a problem that fastjson can identify and reference other jars. What if the server uses fastjson and the client uses gson
1. All use fastjson
2. When converting to JSON, set a property to disable the circular reference object
json=JSON.toJSONString(stuList,SerializerFeature.DisableCircularReferenceDetect);
JSON to list
Sometimes, you don't need all the fields in the object. At this time, you can set a property filter to filter out the fields you don't need
Set name filtering. Please see the log
Introducing a situation
Defines a generic class
There is a student object and a string
Convert object to JSON
When we want to turn this JSON into an object, the problem comes
At this time, you need to implement the typereference class to encapsulate the object
Perfect solution. Typereference can be used for all types with generics
Finally, I will introduce a website to you http://json.cn/ Especially powerful, it will automatically format JSON and report errors if there are syntax errors
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!