0

我有一个 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 然后获取所有图像 url,无论列出多少,并将它们放入分区?

感谢我能得到的任何帮助!

4

1 回答 1

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
}

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

于 2013-09-28T21:35:56.833 回答