如何使用jquery访问这种Json?

时间:2013-02-26 05:35:14

标签: javascript jquery cordova

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

4 个答案:

答案 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),在这种情况下,第一个keyobject是{{ {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

DEMO.

答案 2 :(得分:0)

您可以使用for循环。

for(i = 0; i <= dataJson.length; i++)
{
     console.log(dataJson[i].name);
}

答案 3 :(得分:0)

您可以像

这样的数组访问数字键
console.log(dataJson[1].id);