This period of time we are doing an English translation software small little app, involving the analysis of JSON data, so specifically to summarize!
Suppose we want to parse the following data, in fact, in peacetime, the JSON data returned is very messy, it is difficult to distinguish the relationship between the data, which is after the relevant tools to beautify the results
{
"Translation": [
Love
],
"Basic": {
"Us-phonetic": "L?v",
"Phonetic": "L?v",
"Uk-phonetic": "L?v",
"explains": [
"N. Love, dear, Fond, love,"
"Vt. Be fond of, love, or adore;
"VI. Love",
"N. (love) name; (English) Ralph"
]
},
"Web": [
{
"Value": [
"Love",
"Love",
Love
],
"Key": "Love"
},
{
"Value": [
"Endless Love",
"Blue Life and Death Love",
"No love."
],
"Key": "Endless Love"
},
{
"Value": [
"Puppy Love",
"Adolescent Love",
First Love
],
"Key": "Puppy Love"
}
]
}
We define the data as a string and add the escape character to it, and it's a bit messy, so we'll just have to analyze it.
JSON data is simply a nested set of objects and arrays, so let's get started!
String st = "{\" translation\ ": [\" Love \ "],\" basic\ ": {\" us-phonetic\ ": \" l?v\ ", \" phonetic\ ": \" l?v\ ", \" uk-phonetic\ ": \" L ? v\ ", \" explains\ ": [\" N. Love; loving, Love, "Vt.". Like, love, adore \ ", \" Vi love \ ", \" N. "The name of the person; (English) Ralph's"]},\ "query\": \ "love\", \ " Errorcode\ ": 0,\" web\ ": [{\" value\ ": [\" Love \ "," Love "," Love "],\" key\ ": \" love\ "},{\" value\ ": [\" Endless Love ", \" Blue Life and Death Love ", \" Not Love " "],\" key\ ": \" Endless love\ "},{\" value\ ": [\" Puppy Love \ "," Adolescent Romance ", \" First love \ "],\" key\ ": \" Puppy Love\ "}]}";
Jsonobject str = jsonobject.fromobject (ST); The outermost layer of the JSON data is definitely an object, with the argument as a string
Object STR is also divided into 3 parts: Translation,basic,web
if (Str.has ("translation")) {
jsonarray tr = Str.getjsonarray ("translation"); //translation is a JSON array
for (int i = 0; I<tr.size (); i++) { //array element traversal
system.out.println ( Tr.getstring (i)); // Because the value of the element is of type string, the method of the value is GetString (index)
}
}
There are arrays and elements in basic
if (Str.has ("basic")) {
Jsonobject US = str.getjsonobject ("basic"); Parse the element and output the value of the element
System.out.print ("Mei: [" + us.getstring ("us-phonetic") + "]\t");
Jsonobject UK = Str.getjsonobject ("basic");
System.out.print ("English: [" + us.getstring ("uk-phonetic") + "]\n");
Jsonobject basic = Str.getjsonobject ("basic"); Get the Basic object first
Jsonarray explain = Basic.getjsonarray ("explains"); And get the explains array under the basic object.
for (int i = 0;i<explain.size (); i++) {//traversal of array elements
System.out.println (explain.getstring (i));
}
}
if (Str.has ("Web")) {//web is an array, and each array element is another three JSON object
System.out.println ("expand:");
Jsonarray Web = Str.getjsonarray ("web");
for (int i = 0; I<web.size (); i++) {But arrays are nested within objects
String t = web.getstring (i); //The traversal process assigns a Web array element to a string variable
Jsonobject we = jsonobject.fromobject (t); Object that gets each element through string
if (We.has ("key")) {
System.out.print (we.getstring ("key") + "\ T"); Get the elements in an object
}
if (We.has ("value")) {
Jsonarray value = We.getjsonarray ("value");
for (int x = 0; x<value.size (); x + +) {//iterate over nested arrays in the object
System.out.print (value.getstring (x)); Get the value of an array element
if (X<value.size ()-1) {
System.out.print (";");
}
}
}
System.out.println ();
}
}
I'll finish the analysis here!
JSON data parsing with Java (array of objects nested in each other)