gpt4 book ai didi

javascript - 如何使用jquery访问这种Json?

转载 作者:行者123 更新时间:2023-12-02 18:58:16 25 4
gpt4 key购买 nike

我是 Jquery 和 javascript 的初学者,任何人都可以帮助我如何使用 jquery 访问这种 json 数据。如果有人知道更好的方法来访问此数据,请帮助我。这个 json 数据来自远程 url。

{
"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"
}
}

最佳答案

使用$.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代表 key 和 v代表值(每个 object ),在本例中为第一个 keyobject是( "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']将返回name1等等。如果您使用$.getJSON那么你不需要解析它,因为它会被 jQuery 解析你可以像这样使用它

$.getJSON('url', function(data){
$.each(data, function(k, v){
document.write(v['id'] + ' ' + v['name']+'<br />');
});
});

DEMO.

关于javascript - 如何使用jquery访问这种Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com