Comprehensive analysis of Android basic--json data

Source: Internet
Author: User

This article covers the following topics:

  • What is JSON?
  • Parsing and storage of Jsonobject
  • Parsing and storage of jsonobject (abstract)
  • Parsing and storage of Jsonarray
  • Simulation of the surrounding gas station JSON data combat

Json:javascript Object Notation (JavaScript object Notation)

    • JSON is the syntax for storing and exchanging textual information
    • JSON is a lightweight text data interchange Format
    • JSON independent of language and platform
    • JSON is self-descriptive and easier to understand

XML-like, smaller, faster, and easier to parse than XML

    • No end tag
    • Even shorter
    • Read and write faster
    • Working with arrays
    • Do not use reserved words

JSON syntax is a subset of JavaScript Object notation syntax

    • Data in name/value pairs
    • Data separated by commas
    • Curly braces Save Object
    • square brackets to save data

The JSON value can be

    • Number (integer or floating point)
    • String (in double quotes)
    • Logical value (TRUE or FALSE)
    • Array (in square brackets)
    • Object (in curly braces)
    • Null

Jsonobject data is stored in Key-value, separated by colons, and the outer curly braces represent an object

First, create a bean class that stores data

Below we use the JSON data obtained from the server into a JSON object to get the values in its object

We will encounter other JSON objects nested in a JSON, and this nested JSON object can abstract common attributes, see example

At this point we don't have to create two objects, and create an object to extract their properties.

First, create a bean class that stores data

Then we parse and store

By traversing the Jsonarray, the rest is like Jsonobject,

Here, for example, the surrounding gas station data, the analytical steps are

    • Member variables in the parsing data (curly braces are objects, square brackets are arrays, array is stored using list)
    • Create the corresponding object based on the results of the analysis
    • Parse JSON data, store JSON data

You can try to write your own analysis, find out where is wrong, directly run the program, the system will automatically give you a hint which line to parse the error

I. Member variables in the analysis data

In JSON, there are only two syntaxes, jsonobject (inside curly braces) and jsonarray (in square brackets)

  • Jsonobject: Can be understood as a map collection, get value by get
  • Jsonarray: Can be understood as an array, through the loop to get the corresponding Jsonobject

From the above data can be found that there are five JSON objects, a JSON array, from the outside to the inside analysis

Object 1~4:

Object 5: On the last side

Second, according to the results of analysis, create the corresponding object (according to the sequential from the)

In Jsonobject, the left side is the property, the right is the value

If the value on the right is a jsonarray, it is stored in the object using list< object>, which simply means that the list in the object is nested another object

Remember: curly braces with objects, square brackets with a set

Based on the above analysis, create the first object

Create a second object

Create a third object

Create a fourth object

Create a fifth object

III. parsing JSON data and storing JSON data

Because the data object is a layer of nesting, this analytic thinking and the traversal of the tree is a reason, the middle-aged clothing from the outer layer and the outer layers, so we are in the resolution of the inner layer when we need edge parsing edge to store our data

Parsing and storage of data

Since our fourth object is nested in the third object array, we nest a layer of loops inside

Since the fourth object is a property of the same nature, we abstract it into an object of type and price whose key is indeterminate and needs to be traversed by the keys to get the value

Then we output the results of our analysis.

Comprehensive analysis of Android basic--json data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.