jquery json获取对象的所有部分

时间:2013-09-28 21:33:42

标签: jquery json

我有一个json文件设置了一些像这样的对象:

{
"everfi_commons":{
   "info" : {
               "projTotal" : "everfi_Commons",
       "company" : "Everfi",
       "name" : "Commons",
               "type" : "ipad",
       "description" : "this product, bla bla bla bla bla",
       "folder": "everfi_commons",
               "thumbProjName": "COMMONS",
               "thumbDescription" : "bla bla bla bla bla"
   },
   "images" : {
       "image_1" : "image one url",
       "image_2" : "image two url",
       "image_3" : "image three url",
       "image_4" : "image four url"
   }
},
"project_two":{
   "info" : {
               "projTotal" : "project_two",
       "company" : "Everfi",
       "name" : "Commons",
               "type" : "html5",
       "description" : "this product, bla bla bla bla bla",
       "folder": "project_two",
               "thumbProjName": "COMPANY 2",
               "thumbDescription" : "bla bla bla bla bla"
   },
   "images" : {
       "image_1" : "image one url",
       "image_2" : "image two url",
       "image_3" : "image three url",
       "image_4" : "image four url",
               "image_5" : "image five url"
   }
}
}

我知道如何访问对象的非常具体的部分,但我想知道的是,如果有办法进入everfi_commons.images然后获取所有图像网址,无论列出和放置多少他们变成了一个div?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

不确定。首先将JSON解析为一个对象,然后迭代everfi_commons.images

var data = $.parseJSON(...);
for(image in data.everfi_commons.images) {
    alert(data.everfi_commons.images[image]); // or whatever you want
}

如果通过AJAX调用检索JSON,那么您甚至不需要$.parseJSON,因为如果服务器在响应中发送了正确的Content-Type,jQuery将自动执行此操作。