Android parsing JSON data

Use of jsonobject

1、 Use of JSON objects:

2、 Use of JSON array:

3、 JSON arrays are mixed with JSON objects

4、 Storing objects in JSON arrays

Principle of jsonobject

Storage and retrieval of jsonobject

1、 A LinkedHashMap is maintained in jsonobject. When a parameterless jsonobject is generated, a map is actually initialized:

2、 When jsonobject adds data, it essentially stores the key value pair method of the data in the above map:

3、 Taking data from jsonobject, it is easy to think of taking data from map:

Parsing process of jsonobject

1、 Jsonobject also has a constructor with parameters: it is often used to pass a parameter of type string

2、 Follow in and find that the nextvalue () method of jsontokener is mainly executed, in which the data is mainly parsed;

In the nextcleaninternal method, it will parse character by character from beginning to end and do some processing for some characters. For example, space, line feed, escape character, etc! When parsed to [indicates the start of reading an object, when parsed to {indicates the reading of an array

3、 In the readObject method, you still call the nextcleaninternal () method to get the parsed characters one by one, down to}. Important codes are posted below

4、 Nextvalue method is a key method, which is used to process most of the work of parsing! There is a readliteral method in nextvalue, which processes some types and obtains the parsed results:

5、 The parsing process of jsonarray is the same as that of jsonobject. It maintains a list:

Use of gson

1、 We first add a person class to the test to facilitate the test:

2、 Gson converts objects to JSON format

Print results: {name ":" Linux "," age ": 23}

3、 Gson parses JSON format into objects

Print result: Liu Li, 19

4、 Gson parses the list object into JSON format:

Print results: [{name ":" Name0 "," age ": 0}, {name": "name1", "age: 5}]

5、 Gson parses the JSON format into a list object:

Print result: Name: Linux age: 10 name: huhx age: 22

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>