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