This article mainly introduces four methods of json parsing. If you need it, you can refer to the extensive use of Json in Web development as a carrier for data transmission, it is very common to parse Json returned data. The following describes four ways to parse Json:
Part 1
The Code is as follows:
Var list1 = [1, 3, 4];
Alert (list1 [1]);
Var list2 = [{"name": "leamiko", "xing": "lin"}];
Alert (list2 [0] ["xing"])
Alert (list2 [0]. xing)
Part 2
The Code is as follows:
Var value = {
"China ":{
"Hangzhou": {"item": "1 "},
"Shanghai": {"item": "2 "},
"Chengdu": {"item": "3 "}
},
"America ":{
"Aa": {"item": "1 "},
"Bb": {"item": "2 "}
},
"Spain ":{
"Dd": {"item": "1 "},
"Ee": {"item": "2 "},
"Ff": {"item": "3 "}
}
};
For (var countryObj in value)
{
Document. write (countryObj + ":
")
// Useless for (var cityObj in value. countryObj)
For (var cityObj in value [countryObj])
{
Document. write (''+ cityObj +"
");
For (var itemObj in value [countryObj] [cityObj])
{
Document. write ("" + itemObj + value [countryObj] [cityObj] [itemObj] +"
")
}
}
}
Explanation:
CountryObj is an attribute of the value object. value [countryObj] is the attribute value of the value object. Here it is a json object, such as B, value [countryObj] [cityObj] is the property value of josn object B. It is also a json object, therefore, value [countryObj] [cityObj] ["item"] can obtain the value of the json object that is temporarily c, or value [countryObj] [cityObj]. item.
In short, it is critical to distinguish between json and array.
Part 3
The Code is as follows:
Var value2 = {
"China ":[
{"Name": "hangzhou", "item": "1 "},
{"Name": "shanghai", "item": "2 "},
{"Name": "sichuan", "item": "3 "}
],
"America ":[
{"Name": "aa", "item": "12 "},
{"Name": "bb", "item": "2 "}
],
"Spain ":[
{"Name": "cc", "item": "1 "},
{"Name": "dd", "item": "23 "},
{"Name": "ee", "item": "3 "}
]
};
For (var countryObj in value2)
{
Document. write (countryObj + ":
")
For (var cityObj in value2 [countryObj])
{
// You can use document. write ("" + value2 [countryObj] [cityObj]. item +"
");
Document. write (cityObj + "" + value2 [countryObj] [cityObj] ["name"] +"
");
}
}
Explanation:
CountryObj is the property name of the value2 object, and value2 [countryObj] is the value of the value2 object. In this example, it is an array, cityObj is an element of the array, and it is another json object, therefore, value2 [countryObj] [cityObj] ["name"] can access the attribute value of the name of this object, or through value2 [countryObj] [cityObj]. to access the attribute value.
Part 4
The Code is as follows:
Var value2 = {
"China ":[
{"Name": "hangzhou", "item": "1 "},
{"Name": "shanghai", "item": "2 "},
{"Name": "sichuan", "item": "3 "}
],
"America ":[
{"Name": "aa", "item": "12 "},
{"Name": "bb", "item": "2 "}
],
"Spain ":[
{"Name": "cc", "item": "1 "},
{"Name": "dd", "item": "23 "},
{"Name": "ee", "item": "3 "}
]
};
For (var countryObj in value2)
{
Document. write (countryObj + ":
")
// Document. write ("" + value2 [countryObj]. length );
For (var I = 0; I <value2 [countryObj]. length; I ++)
{
Document. write ("" + value2 [countryObj] [I] ["name"] +"
");
}
}
Explanation:
The attribute name of the countryObj value2 object. The value2 [countryObj] attribute value is an array in this example. value2 [countryObj]. length: the length of the array. The value = json object in the value2 [countryObj] [I] array.
Value2 [countryObj] [I] ["name"] obtains the name value. You can also use value2 [countryObj] [I]. name to obtain the name value.