我試過100個不同的東西,並花費數天時間查看Google和Stackoverflow,但我找不到解決此問題的方法。我在此API響應正文後調用的所有內容都會返回 undefined!通過嵌套的Javascript對象從API響應迭代
從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"
}
}"
}
]
這個響應是從批調用。如果我分開給他們打電話,我可以輕鬆地遍歷答案,並從中得到一切。當我在批處理中調用它們時,我無法通過「正文」,而需要使用批處理調用。
console.log(response[0].body);
將返回該對象的體的響應的第一部分的內部,但是console.log(response[0].body.data);
返回未定義。我只是不明白。這應該很簡單,但就像門上有鎖,我沒有正確的鑰匙。
我通常沒有問題遍歷對象,所以我不需要一個廣義的答案。無論我在這裏看到什麼,我都需要幫助。爲什麼控制檯顯示 undefined當我打電話正文之後的任何內容時,我需要做些什麼才能獲得這些值?
這些'body'值只是巨型琴絃 - 本質上嵌入JSON。你必須單獨解析它們作爲JSON來獲得它們的內部價值。 – jfriend00 2014-11-01 03:50:39