收集json解析的四种方法分享_javascript技巧

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

Json在Web开发的用处非常广泛,作为数据传递的载体,如何解析Json返回的数据是非常常用的。下面介绍下四种解析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 + ":
")
//没用的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] +"
")
}
}
}

解释:

countryObj 为value对象的一个属性明,value[countryObj] 为value对象的属性值 这里为一个json对象比如b,value[countryObj][cityObj]为josn对象b的属性值 它也是一个json对象,于是 value[countryObj][cityObj]["item"]便可以取到json对象暂时成为c的值,或者 value[countryObj][cityObj].item。

总之分清是json还是array这是很关键的。

Part 3

复制代码代码如下:

var value2 = {

"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])
{
//可以用document.write(" " + value2[countryObj][cityObj].item + "
");
document.write(cityObj + " " + value2[countryObj][cityObj]["name"] + "
" );
}
}

解释:

countryObj 为value2对象的属性名,value2[countryObj] 为value2对象属性值 在本例中它是一个数组,cityObj 是数组的一个元素,它又是另外一个json对象,于是,value2[countryObj][cityObj]["name"]就访问到该对象的 name的属性值,也可以通过 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":"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 + ":
")
//document.write(" " + value2[countryObj].length);
for (var i = 0;i {
document.write(" " + value2[countryObj][i]["name"] + "
");
}
}

解释:

countryObj value2对象的属性名,value2[countryObj] 属性值 本例中是一个数组,value2[countryObj].length 数组的长度,value2[countryObj][i]数组的项 == json对象。

value2[countryObj][i]["name"] 取得name的值,也可以用value2[countryObj][i].name 来取得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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!