Home > Article > Web Front-end > Collect four methods to share json parsing_javascript skills
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
Part 2
In short, it is crucial to distinguish whether it is json or array.
Part 3
Copy code
],
"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 attribute name of the value2 object, value2[countryObj] is the attribute 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, so, value2[countryObj] [cityObj]["name"] can access the attribute value of the object's name, or you can access the attribute value through value2[countryObj][cityObj].name.
Part 4
The attribute name of the countryObj value2 object, value2[countryObj] attribute value. In this example, it is an array, value2[countryObj].length is the length of the array, and the items of the value2[countryObj][i] array == json object.
value2[countryObj][i]["name"] gets the value of name. You can also use value2[countryObj][i].name to get the value of name.