【发布时间】:2014-11-01 03:48:07
【问题描述】:
我尝试了 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,我需要做什么才能获得这些值?
【问题讨论】:
-
这些
body值只是巨大的字符串——本质上是嵌入的JSON。您必须将它们单独解析为 JSON 以获取它们的内部值。
标签: javascript arrays object javascript-objects facebook-sdk-4.0