Home > Web Front-end > JS Tutorial > Collect four methods to share json parsing_javascript skills

Collect four methods to share json parsing_javascript skills

WBOY
Release: 2016-05-16 17:03:31
Original
963 people have browsed it

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

Copy code The code is 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 The code is as follows:

var value = {
"china":{
"hangzhou":{"item":"1"},
"shanghai":{"item":"2"},
"chengdu ":{"item":"3"}
},
"America":{
"aa":{"item":"1"},
"bb":{" item":"2"}                                                                                                      2"},
"ff":{"item":"3"}
}
};
for(var countryObj in value)
{
document.write( countryObj ":
")
//Useless for(var cityObj in value.countryObj)
for(var cityObj in value[countryObj])
{
document. write(' ' cityObj "
");
for(var itemObj in value[countryObj][cityObj])
{
document.write(" " itemObj value[countryObj][ cityObj][itemObj] "
")
}
} }
}



Explanation:
countryObj is an attribute value of the value object, value[countryObj] is the attribute value of the value object, here is a json object such as b, value[countryObj][cityObj] is the attribute value of the josn object b, which is also a json object. So value[countryObj][cityObj]["item"] can get the json object to temporarily become the value of c, or value[countryObj][cityObj].item.

In short, it is crucial to distinguish whether it is json or array.

Part 3

Copy code

"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 ":
")
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

Copy code The code is as follows:

var value2 = {
"china":[
{"name":"hangzhou", "item":"1"},
"china":[
{"name":"shanghai", "item":"2 "},
{"name":"sichuan", "item":"3"}
],
"America":[
{"name":"aa", "item ":"12"},
                                                                                                                                                   ", "item":"1"},
{"name":"dd", "item":"23"},
{"name":"ee", "item":"3 "}
]
};

for (var countryObj in value2)
{
document.write(countryObj ":
")
/ /document.write(" " value2[countryObj].length);
for (var i = 0;i < value2[countryObj].length; i )
{
document.write(" " value2[countryObj][i]["name"] "
");
}
}


Explanation:

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template