Four ways to collect JSON parsing

Source: Internet
Author: User

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. Here are the next four ways to parse JSON:

Part 1 Yun Ding Casino
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.

Four ways to collect JSON parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.