0

我是 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"
    }
}
4

4 回答 4

1

一旦你解析了 JSON,它就是一个普通的 JS 对象:

for (var id in data) {
    var thing = data[id];

    console.log(thing.name);
}
于 2013-02-26T05:37:38.717 回答
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),在这种情况下,第一个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']您可以检索1v['name']将返回name1等等。如果你使用$.getJSON那么你不需要解析它,因为它会被解析jQuery,你可以像这样使用它

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

演示。

于 2013-02-26T05:48:32.680 回答
0

你可以使用for循环。

for(i = 0; i <= dataJson.length; i++)
{
     console.log(dataJson[i].name);
}
于 2013-02-26T05:39:09.567 回答
0

您可以将数字键作为数组访问,例如

console.log(dataJson[1].id);
于 2013-02-26T05:40:27.210 回答