2013-02-26 26 views
0

我开始在jquery和javascript可以任何人帮助我如何访问这种使用jquery.json的数据如果任何人有想法更好的方式来访问这个数据比请帮助我。这json数据来自远程url。如何使用jquery访问这种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

那你试试? – mplungjan 2013-02-26 05:36:42

+0

“如果任何人有更好的方式来访问这些数据比请帮助我”。你的方式是什么?你有什么尝试?请发布您的当前代码以帮助改进它。 – ryadavilli 2013-02-26 05:36:54

+0

jQuery.each将帮助 – 2013-02-26 05:36:54

回答

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 represnts值(各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']可以retrive的1,v['name']将返回name1等。如果你使用$.getJSON那么你并不需要解析它,因为它会通过jQuery解析,您可以使用它像

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

DEMO.

1

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

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

    console.log(thing.name); 
} 
+0

但如何获取数据?我有远程网址。 – 2013-02-26 05:42:51

+0

@AmitPrajapati:'$ .getJSON'? – Blender 2013-02-26 05:44:09

+0

好的,我会试试这个谢谢亲爱的@Blender – 2013-02-26 05:47:16

0

可以使用for循环。

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

您可以访问数字键的排列像

console.log(dataJson[1].id);