简体   繁体   中英

Iterate through nested Javascript Objects from API response

I've tried 100 different things, and spend days looking through Google and Stackoverflow, but I can't find a solution to this problem. Everything I call after the body of this API response returns undefined !

The response from Facebook SDK looks like this:

[
    {
        "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"  
            }
        }"
    }
]

This response is from a batch call. If I call them separately, I can easily iterate through the responses, and get everything from them. When I call them in the batch though, I can't get past "body", and I need to use a batch call.

console.log(response[0].body); will return the object inside the of the first part of the response, but console.log(response[0].body.data); 的对象,但是console.log(response[0].body.data); returns undefined . I just don't get it. This should be simple but it's like there's a lock on the door and I don't have the right key.

I normally have no issue iterating through objects, so I don't need a generalized answer. I need help seeing whatever it is here that I don't see. Why does the console show undefined when I call anything after the , and what do I need to be doing to get any of these values? 后面调用任何东西时,为什么控制台显示未定义 ,为了获得这些值,我需要做些什么?

That JSON contains nested JSON. body seems to be a string. Use

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

The values from the body are just strings.which are embedded as json.So firstly you would need to parse them using JSON.parse .

The code would be like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM