我已经尝试了 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。我只是不明白。这应该很简单,但就像门上有锁,但我没有正确的 key 。
我通常在遍历对象时没有问题,所以我不需要一个通用的答案。我需要帮助才能看到我在这里看不到的任何东西。为什么当我在 body 之后调用任何内容时控制台显示 undefined,我需要做什么才能获得这些值?
最佳答案
该 JSON 包含嵌套的 JSON。 body 似乎是一根绳子。使用
var body = JSON.parse(response[0].body);
关于javascript - 从 API 响应迭代嵌套的 Javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26686028/