1

我试过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当我打电话正文之后的任何内容时,我需要做些什么才能获得这些值?

+1

这些'body'值只是巨型琴弦 - 本质上嵌入JSON。你必须单独解析它们作为JSON来获得它们的内部价值。 – jfriend00 2014-11-01 03:50:39

回答

2

JSON包含嵌套的JSON。身体似乎是一个字符串。使用

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

非常感谢!我甚至不知道一个响应是一个字符串。那有什么意义呢? – 2014-11-01 04:04:51

+1

看看下面的文章,它会完全回答你的问题:http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/ – istos 2014-11-01 04:07:35

+0

@istos绝对解释的东西,并且很有意义。 – 2014-11-01 04:31:18

1

值从身体只是strings.which嵌入作为json.So首先你需要使用JSON.parse解析他们。

的代码会像

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

谢谢!有人在你面前回答同样的问题,所以我将其中的一个标记为答案。我得到了我需要的东西,所以谢谢你! – 2014-11-01 04:04:22

+1

很好...... :)。哈皮编码 – 2014-11-01 04:05:56