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