Summary of several methods of traversing json to obtain data
- 2021-07-13 03:58:50
- OfStack
Json is widely used in Web development. As a carrier of data transmission, how to parse the data returned by Json is very common. Here are four ways to parse Json:
Part 1
var list1 = [1,3,4];
alert(list1[1]);
var list2 = [{"name":"leamiko","xing":"lin"}];
alert(list2[0]["xing"])
alert(list2[0].xing)
Part 2
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 + ":<br />")
// It's useless for(var cityObj in value.countryObj)
for(var cityObj in value[countryObj])
{
document.write(' ' + cityObj + "<br />");
for(var itemObj in value[countryObj][cityObj])
{
document.write(" "+ itemObj + value[countryObj][cityObj][itemObj] +"<br />")
}
}
}
Explanation:
countryObj is an attribute of value object, value [countryObj] is the attribute value of value object, here is an json object such as b, value [countryObj] [cityObj] is the attribute value of b object of josn object, which is also an json object, so value [countryObj] [cityObj] ["item"] can get the value that json object temporarily becomes c, or value [countryObj] [cityObj].
In a word, it is crucial to distinguish between json and array.
Part 3
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"}
]
};<BR>
for (var countryObj in value2)
{
document.write(countryObj + ":<br />")
for (var cityObj in value2[countryObj])
{
// Can be used document.write(" " + value2[countryObj][cityObj].item + "<br />");
document.write(cityObj + " " + value2[countryObj][cityObj]["name"] + "<br />" );
}
}
Explanation:
countryObj is the property name of the value2 object, value2 [countryObj] is the property value of the value2 object, in this case it is an array, cityObj is an element of the array, and it is another json object, so value2 [countryObj] [cityObj] ["name"] accesses the property value of the name of the object, or it can be accessed through value2 [countryObj] [cityObj]. name.
Part 4
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 + ":<br />")
//document.write(" " + value2[countryObj].length);
for (var i = 0;i < value2[countryObj].length; i++)
{
document.write(" " + value2[countryObj][i]["name"] + "<br />");
}
}
Explanation:
countryObj value2 object property name, value2 [countryObj] property value, in this case an array, value2 [countryObj]. length array length, value2 [countryObj] [i] array entries = = json object.
value2 [countryObj] [i] ["name"] takes the value of name, or value2 [countryObj] [i]. name can be used to take the value of name.
When a person can't find a way out, the best way is to do what he can do well at present to the extreme, so that no one can reach it.