Android JSON conversion

Source: Internet
Author: User

Extracting data from JSON

JSON data

{
"Name": "Tbwisk",
"Phone": {
"One": "110",
"Both": "119"
},
"Array": [
{
"Number": "323"
},
{
"Number": "666"
},
{
"Number": "455"
}
]
}

It's basically just jsonobject and Jsonarray.

First, the JSON data is saved with String one

Jsonobject json = new Jsonobject (one);

String name = json.getstring ("name")//extract parameter name corresponding data

Jsonobject phone = json.getjsonobject ("Phone");

String one = phone.getstring ("one");

String one = phone.getstring ("the");//now extracts the corresponding data from the phone

Here is the data extracted from the JSON data

Jsonarray array = json.getjsonarray ("array")

Array data is contained inside the array.

The last is to extract the array data, usually using the array of JSON data key values are the same

for (int i=0;i<array.length (); i++) {

Jsonobject item = array.getjsonobject (i);

String number = item.getstring ("number");

System.out.println ("number =" +number "); Array data is then extracted, of course, sometimes you need to declare a list in advance to save the data

}

Above is extracting the data from the JSON.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////

The following is how to generate a JSON data

Just generate the above data.

First of all

Jsonobject json = new Jsonobject ();

Json.put ("name", "Tbwisk");

Jsonobject phone= new Jsonobject ();

Phone.put ("One", "110");

Phone.put ("A", "119");

Json.put ("Phone", phone);

Jsonarray array = new Jsonarray ();

Jsonobject number = new Jsonobject ();

Number.put ("number", "323");

Array.put (number);

Number.put ("number", "666");

Array.put (number);

Number.put ("number", "455");

Array.put (number); Then the array is generated, and finally the array is added to the JSON

Json.put ("array", array);

Then the JSON data on the top is generated.

Android JSON conversion

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.