通过API响应迭代嵌套的Javascript对象

时间:2014-11-01 03:48:07

标签: javascript arrays object javascript-objects facebook-sdk-4.0

我尝试了100种不同的东西,花了几天时间浏览Google和Stackoverflow,但我无法找到解决此问题的方法。我在API响应主体后调用的所有内容都返回 undefined

Facebook SDK的回复如下:

[
    {
        "body": "[   
            "data": [
                {
                    "name": "Larry Syid Wright",
                    "administrator": false,
                    "id": "xxx"
                },      {         
                    "name": "Melissa Long Jackson",
                    "administrator": false,
                    "id": "xxx"
                },      {       
                    "name": "Charlotte Masson",
                    "administrator": false,
                    "id": "xxx"  
                }  
            ],   
            "paging": {      
                "next": "url"
            }
        ]"
    },{
        "body": "{   
            "data": [      
                {         
                    "id": "xxx_xxx",       
                    "message": "In honor of Halloween, how many of you have your own ghost stories?  Who believes in ghosts and who doesn't?",                 
                    "type": "status",         
                    "created_time": "2014-10-31T20:02:01+0000",         
                    "updated_time": "2014-11-01T02:52:51+0000",         
                    "likes": {            
                        "data": [               
                            {                  
                                "id": "xxx",                  
                                "name": "Joe HerBatman Owenby Jr." 
                            }          
                        ],            
                    }
                    "paging": {               
                        "cursors": 
                            {                  
                                "after": "xxx",                  
                                "before": "xxx"               
                            }            
                        }         
                    }
                },{         
                    "id": "xxx_xxx",         
                    "from": {            
                        "id": "xxx",            
                        "name": "Jessica Starling"         
                    },       
                    "message": "Watching the "Campaign" and I can't help but notice what a fantastic job they did (Will ferrell and all) with that North Carolina accent! Ya'll know we sound different than other southern states ;)",         
                    "type": "status",           
                    "created_time": "2014-11-01T02:36:21+0000",         
                    "updated_time": "2014-11-01T02:36:21+0000",         
                    "likes": {            
                        "data": [               
                            {                  
                                "id": "xxx",                  
                                "name": "Scott Williams"n               
                            }         
                        ]
                    }      
                }      
            ],   
            "paging": {      
                "previous": "xxx",      
                "next": "xxx"  
            }
        }"
    }
]

此回复来自批量调用。如果我单独调用它们,我可以轻松地遍历响应,并从中获取所有内容。当我在批量中给他们打电话时,我无法通过" body",我需要使用批量电话。

console.log(response[0].body);将返回响应第一部分的 body 内的对象,但console.log(response[0].body.data);返回 undefined 。我只是没有得到它。这应该很简单,但就像锁上门一样,我没有正确的钥匙。

我通常没有问题迭代对象,所以我不需要一个通用的答案。我需要帮助,看看我在这里看不到的任何东西。当我在 body 之后调用任何内容时,为什么控制台显示 undefined ,为了获得这些值,我需要做些什么?

2 个答案:

答案 0 :(得分:2)

JSON包含嵌套的JSON。身体似乎是一个字符串。使用

var body = JSON.parse(response[0].body);

答案 1 :(得分:1)

正文中的值只是字符串。它们嵌入为json。首先,您需要使用JSON.parse解析它们。

代码就像

var body = JSON.parse(response[0].body);