我是Jquery的初学者和javascript可以任何人帮助我如何使用jquery.if访问这种json数据任何人都有想法更好地访问这些数据比请帮助我。这个json数据来自远程网址。
{
"1": {
"id": "1",
"name": "name1",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1_thumb.jpg"
},
"2": {
"id": "2",
"name": "name2",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model2.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model2_thumb.jpg"
},
"3": {
"id": "3",
"name": "name3",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model3.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model3_thumb.jpg"
},
"4": {
"id": "4",
"name": "name4",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model4.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model4_thumb.jpg"
},
"5": {
"id": "8",
"name": "Name8",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model8.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model8_thumb.jpg"
}
}
答案 0 :(得分:1)
一旦解析了JSON,它就只是一个常规的JS对象:
for (var id in data) {
var thing = data[id];
console.log(thing.name);
}
答案 1 :(得分:1)
使用$.jsonParse解析json字符串,然后使用$.each
循环$.each(data, function(k, v){
document.write(v['id'] + ' ' + v['name']+'<br />');
});
<强>输出:强>
1 name1
2 name2
3 name3
4 name4
8 Name8
在上面的示例中,k
表示密钥和v
代理值(每个object
),在这种情况下,第一个key
和object
是{{ {1}}是关键,"1"
是对象)
{...}
使用"1": {
"id": "1",
"name": "name1",
"large_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1.jpg",
"thumb_name": "http://critiques-demo.she.com/beauty/makeuptool/images/models/model1_thumb.jpg"
}
,您可以检索v['id']
,1
将返回v['name']
,依此类推。如果您使用$.getJSON,那么您不需要解析它,因为它将由name1
解析,您可以像
jQuery
答案 2 :(得分:0)
您可以使用for
循环。
for(i = 0; i <= dataJson.length; i++)
{
console.log(dataJson[i].name);
}
答案 3 :(得分:0)
您可以像
这样的数组访问数字键console.log(dataJson[1].id);