我试过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