标签:
首先温习一下JSON格式:
(1) 单个对象{"变量名1":"变量值1","变量名2":"变量值2"}
(2) 数组[{ "变量名11":"变量值1","变量名12":"变量值2" },{ "变量名21":"变量值1","变量名22":"变量值2" },{ "变量名31":"变量值1","变量名32":"变量值2" }]
===============================================================
情况1:
设返回的数据字符串为data,
则JS代码解析JSON:var d = eval(‘(‘+data+‘)‘);
取变量名1的值:alert(d[‘变量名1‘]);
结果:弹出显示"变量值1"
===============================================================
情况2:设返回的数据字符串为data,
则JS代码解析JSON:var d = eval(‘(‘+data+‘)‘);
取变量名21的值:alert(d[1][‘ 变量名21 ‘]);
结果:弹出显示"变量值1"
===============================================================
举个小例子看看:
js 代码
function showJSON() { var user = { "username":"andy", "age":20, "info": { "tel": "123456", "cellphone": "98765"}, "address": [ {"city":"beijing","postcode":"222333"}, {"city":"newyork","postcode":"555666"} ] } alert(user.username); alert(user.age); alert(user.info.cellphone); alert(user.address[0].city); alert(user.address[0].postcode); }
这表示一个user对象,拥有username, age, info, address 等属性。
标签:
原文地址:http://www.cnblogs.com/zknublx/p/5854147.html