JSON is very useful in web development, and as a carrier of data transmission, how to parse the data returned by JSON is very common in the World Casino. Here are the next 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/>")//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/>")}}}
Explain:
Countryobj is a property value of the value object stating that Value[countryobj] is the property values of the value object here is a JSON object such as B,value[countryobj][cityobj] for the property value of the Josn object B It is also a JSON object, so value[countryobj][cityobj]["item" can take the JSON object temporarily to be the value of C, or Value[countryobj][cityobj].item.
It is critical 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": "},{" "name": "BB", "Item": "2"}], "Spain": [{"Name": "CC", "Item": "1"},{"name": " DD "," Item ":"},{"" Name ":" EE "," Item ":" 3 "}]}; for (Var countryobj in value2) {document.write (Countryobj + ": <br/>") for (Var cityobj in value2[countryobj]) {/ /You can use document.write (" " + Value2[countryobj][cityobj].item + "<br/>");d ocument.write (cityobj + " " + value2[countryobj][cityobj]["name"] + "<br/>");} }
Explain:
Countryobj is the property name of the Value2 object, Value2[countryobj] is the Value2 object property value 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 object's name, or it can access the property value 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": "},{" "name": "BB", "Item": "2"}], "Spain": [{"Name": "CC", "Item": "1"},{"name": " DD "," Item ":"},{"" 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/>");} }
Explain:
Countryobj value2 object's property name, Value2[countryobj] property value in this example is an array, the length of the value2[countryobj].length array, the item of the Value2[countryobj][i] array = = JSON object.
value2[countryobj][i]["name"] Gets the value of name, or you can use Value2[countryobj][i].name to get the value of name.
How to parse the data returned by JSON