Four ways to collect JSON parsing share _javascript skills

Source: Internet
Author: User

JSON is very useful for web development, and as a carrier of data delivery, how to parse the data returned by JSON is very common. Here are the next four ways to parse JSON:

Part 1

Copy Code code 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

Copy Code code as follows:

var value = {
"A": {
"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 an attribute of the value object, Value[countryobj] is the property value of the value object for a JSON object such as B,value[countryobj][cityobj] for josn object B It is also a JSON object, so value[countryobj][cityobj]["item" can take the value of a JSON object to temporarily become C, or Value[countryobj][cityobj].item.

It is critical to distinguish between JSON and array.

Part 3

Copy Code code as follows:

var value2 = {

"A": [
{"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/>")
for (Var cityobj in value2[countryobj])
{
can be used document.write ("" + Value2[countryobj][cityobj].item + "<br/>");
document.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 be accessed by Value2[countryobj][cityobj].name.

Part 4

Copy Code code as follows:

var value2 = {
"A": [
{"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/>");
}
}

Explain:

Countryobj value2 object's property name, Value2[countryobj] property value in this case is an array, value2[countryobj].length the length of the array, Value2[countryobj][i] The item of the 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.

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.