2012-12-03 60 views
0

我需要你的幫助。我可以正確解析graph.facebook.com/me/feed JSON,但是我無法通過graph.facebook.com/me/conversations來完成。你能幫我麼?這是我如何得到飼料的最後一條消息解析JSON Facebook對話XCODE

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                     options:kNilOptions 
                     error:nil]; 
        NSLog(@"json %@", json); 
        NSDictionary *items = [json objectForKey:@"data"]; 
        // NSDictionary *item = [items objectAtIndex:1]; 

        NSLog(@"items %@", items); 


        NSMutableArray *story = [NSMutableArray array]; 

        for (NSDictionary *item in items) 
        { 

         if([item objectForKey:@"message"] || [item objectForKey:@"message"] != nil || [[item objectForKey:@"message"] length]>0) 
         { 
          [story addObject:[item objectForKey:@"message"]]; 
         } 
         else{ 

          NSLog(@"no message"); 
         } 

        } 
        NSLog(@"story %@", story); 

        NSString *lastMessage = [story objectAtIndex:0]; 
        NSLog(@"lastmessage received is %@", lastMessage); 

你能幫我嗎?謝謝

編輯:我得到這個錯誤在NSString * lastMessage = [故事objectAtIndex:0];

*終止應用程序由於未捕獲的異常 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引0超出界限爲 空數組' *第一擲調用堆棧:(0x341fc2a3 0x3aede97f 0x34147b75 0xc3a1d 0x3add2d91 0x3c22c11f 0x3c22fecf 0x3c22fdc1 0x3c23091d 0x3c230ac1 0x3ae54a11 0x3ae548a4)的libC++ abi.dylib:終止叫做拋出異常

的NSLog從 「項目」 是以下:

(
     { 
     "can_reply" = 1; 
     id = "t_id.xx5719286166414"; 
     "is_subscribed" = 1; 
     link = "http://www.facebook.com/messages/?action=read&tid=id.xx5719286166414"; 
     "message_count" = 4; 
     messages =   { 
      data =    (
           { 
        "created_time" = "2012-11-30T17:53:00+0000"; 
        from =      { 
         email = "[email protected]"; 
         id = 1367741936; 
         name = " xxx xxx"; 
        }; 
        id = "m_msg.fe83b318fa94e6bbcc90a9a52fa4645b01"; 
        message = "Questo \U00e8 un messaggio"; 
        tags =      { 
         data =       (
                 { 
           name = inbox; 
          }, 
                 { 
           name = messenger; 
          }, 
                 { 
           name = read; 
          }, 
                 { 
           name = "source:mobile"; 
          } 
         ); 
        }; 
        to =      { 
         data =       (
                 { 
           email = "[email protected]"; 
           id = 100004765716168; 
           name = "AccountDevelopment Development"; 
          }, 
                 { 
           email = "[email protected]"; 
           id = 1367741936; 
           name = " xxx xxx"; 
          } 
         ); 
        }; 
       }, 
           { 
        "created_time" = "2012-11-29T11:49:00+0000"; 
        from =      { 
         email = "[email protected]"; 
         id = 1367741936; 
         name = " xxx xxx"; 
        }; 
        id = "m_id.128631660627297"; 
        message = "funziona?"; 
        tags =      { 
         data =       (
                 { 
           name = inbox; 
          }, 
                 { 
           name = read; 
          }, 
                 { 
           name = "source:web"; 
          } 
         ); 
        }; 
        to =      { 
         data =       (
                 { 
           email = "[email protected]"; 
           id = 100004765716168; 
           name = "AccountDevelopment Development"; 
          }, 
                 { 
           email = "[email protected]"; 
           id = 1367741936; 
           name = " xxx xxx"; 
          } 
         ); 
        }; 
       }, 
           { 
        "created_time" = "2012-11-28T18:05:57+0000"; 
        from =      { 
         email = "[email protected]"; 
         id = 1367741936; 
         name = " xxx xxx"; 
        }; 
        id = "m_id.141590452655798"; 
        message = "prova messaggio"; 
        tags =      { 
         data =       (
                 { 
           name = inbox; 
          }, 
                 { 
           name = read; 
          }, 
                 { 
           name = "source:web"; 
          } 
         ); 
        }; 
        to =      { 
         data =       (
                 { 
           email = "[email protected]"; 
           id = 100004765716168; 
           name = "AccountDevelopment Development"; 
          }, 
                 { 
           email = "[email protected]"; 
           id = 1367741936; 
           name = " xxx xxx"; 
          } 
         ); 
        }; 
       }, 
           { 
        "created_time" = "2012-11-28T18:04:50+0000"; 
        from =      { 
         email = "[email protected]"; 
         id = 1367741936; 
         name = " xxx xxx"; 
        }; 
        id = "m_id.xx5719286166414"; 
        message = "Ciao Francesco, come stai?"; 
        tags =      { 
         data =       (
                 { 
           name = inbox; 
          }, 
                 { 
           name = read; 
          }, 
                 { 
           name = "source:web"; 
          } 
         ); 
        }; 
        to =      { 
         data =       (
                 { 
           email = "[email protected]"; 
           id = 100004765716168; 
           name = "AccountDevelopment Development"; 
          }, 
                 { 
           email = "[email protected]"; 
           id = 1367741936; 
           name = " xxx xxx"; 
          } 
         ); 
        }; 
       } 
      ); 
      paging =    { 
       next = "https://graph.facebook.com/t_id.xx5719286166414/messages?access_token=BAABlotMxL1MBACbrnuvICOwBKjkw68ZBaqLOY2MVE6tga8xPmiF9VWH7RZAZCoZBDeSD29KWTzuze3xpjP7xb1kmfyw7VYtvGZC0E1gwgJthpZAoKoFXAYanGcBMb1qzO7SBAI3WPLcLAq7yAUpyF9NiHY0vIpsiGJZASx3eZCA83Lm18hpjmo0K97ZBSOiAoYeZCm2vYDgqq4S8jqOvZCvGZCZByNeg4ElNiSwZA1hHqdA6qrFAZDZD&limit=25&until=1354125890&__paging_token=m_id.xx5719286166414"; 
       previous = "https://graph.facebook.com/t_id.xx5719286166414/messages?access_token=BAABlotMxL1MBACbrnuvICOwBKjkw68ZBaqLOY2MVE6tga8xPmiF9VWH7RZAZCoZBDeSD29KWTzuze3xpjP7xb1kmfyw7VYtvGZC0E1gwgJthpZAoKoFXAYanGcBMb1qzO7SBAI3WPLcLAq7yAUpyF9NiHY0vIpsiGJZASx3eZCA83Lm18hpjmo0K97ZBSOiAoYeZCm2vYDgqq4S8jqOvZCvGZCZByNeg4ElNiSwZA1hHqdA6qrFAZDZD&limit=25&since=1354297980&__paging_token=m_msg.fe83b318fa94e6bbcc90a9a52fa4645b01&__previous=1"; 
      }; 
     }; 
     participants =   { 
      data =    (
           { 
        email = "[email protected]"; 
        id = 1367741936; 
        name = " xxx xxx"; 
       }, 
           { 
        email = "[email protected]"; 
        id = 100004765716168; 
        name = "AccountDevelopment Development"; 
       } 
      ); 
     }; 
     senders =   { 
      data =    (
           { 
        email = "[email protected]"; 
        id = 1367741936; 
        name = " xxx xxx"; 
       } 
      ); 
     }; 
     snippet = "Questo \U00e8 un messaggio"; 
     tags =   { 
      data =    (
           { 
        name = inbox; 
       }, 
           { 
        name = read; 
       }, 
           { 
        name = seen; 
       }, 
           { 
        name = "source:web"; 
       } 
      ); 
     }; 
     "updated_time" = "2012-11-30T17:53:00+0000"; 
    } 
) 
+0

你會得到什麼錯誤? – combinatorial

+0

我編輯了我的帖子。謝謝。 – accountDevelopment

回答

0

您在訪問器中缺少一個級別。 JSON中的頂級項目是messages,其中包含data。然後data是一個數組,而不是字典。所以你應該把第一行改爲...

NSArray *messageGroups = [json objectForKey:@"data"]; 
NSDictionary* firstMessageGroup = [messageGroups objectAtIndex:0]; 
NSDictionary *messages = [firstMessageGroup objectForKey:@"messages"]; 
NSArray* items = [messages [email protected]"data"]; 
+0

感謝您的回答,但它不起作用。這是我得到的: '代碼 2012-12-04 18:35:38.126 WebView [700:5a03] items(null) 2012-12-04 18:35:38.129 WebView [700:5a03] story( ) 2012-12-04 18:35:38.132 WebView [700:5a03] ***由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:index 0超出界限陣」 ***第一擲調用堆棧: (0x335332a3 0x3a21597f 0x3347eb75 0xdb9cd 0x3a109d91 0x3b56311f 0x3b566ecf 0x3b566dc1 0x3b56791d 0x3b567ac1 0x3a18ba11 0x3a18b8a4) 的libC++ abi.dylib:終止稱爲拋出異常' – accountDevelopment

+0

不'messages'包含有效的對象。你可以登錄嗎? – combinatorial

+0

'2012-12-05 10:24:20.372 WebView [191:6a03] messages(null)' – accountDevelopment