解析 JSON Facebook 对话 XCODE

Posted

技术标签:

【中文标题】解析 JSON Facebook 对话 XCODE【英文标题】:Parse JSON Facebook Conversations XCODE 【发布时间】:2012-12-03 18:45:00 【问题描述】:

我需要你的帮助。我可以正确解析 graph.facebook.com/me/feed JSON,但我不能用 graph.facebook.com/me/conversations 来解析。你能帮我么?这就是我从提要中获取最后一条消息的方式

 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 = [story 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 = "1367741936@facebook.com";
                        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 = "100004765716168@facebook.com";
                                id = 100004765716168;
                                name = "AccountDevelopment Development";
                            ,
                                                        
                                email = "1367741936@facebook.com";
                                id = 1367741936;
                                name = " xxx xxx";
                            
                        );
                    ;
                ,
                                
                    "created_time" = "2012-11-29T11:49:00+0000";
                    from =                     
                        email = "1367741936@facebook.com";
                        id = 1367741936;
                        name = " xxx xxx";
                    ;
                    id = "m_id.128631660627297";
                    message = "funziona?";
                    tags =                     
                        data =                         (
                                                        
                                name = inbox;
                            ,
                                                        
                                name = read;
                            ,
                                                        
                                name = "source:web";
                            
                        );
                    ;
                    to =                     
                        data =                         (
                                                        
                                email = "100004765716168@facebook.com";
                                id = 100004765716168;
                                name = "AccountDevelopment Development";
                            ,
                                                        
                                email = "1367741936@facebook.com";
                                id = 1367741936;
                                name = " xxx xxx";
                            
                        );
                    ;
                ,
                                
                    "created_time" = "2012-11-28T18:05:57+0000";
                    from =                     
                        email = "1367741936@facebook.com";
                        id = 1367741936;
                        name = " xxx xxx";
                    ;
                    id = "m_id.141590452655798";
                    message = "prova messaggio";
                    tags =                     
                        data =                         (
                                                        
                                name = inbox;
                            ,
                                                        
                                name = read;
                            ,
                                                        
                                name = "source:web";
                            
                        );
                    ;
                    to =                     
                        data =                         (
                                                        
                                email = "100004765716168@facebook.com";
                                id = 100004765716168;
                                name = "AccountDevelopment Development";
                            ,
                                                        
                                email = "1367741936@facebook.com";
                                id = 1367741936;
                                name = " xxx xxx";
                            
                        );
                    ;
                ,
                                
                    "created_time" = "2012-11-28T18:04:50+0000";
                    from =                     
                        email = "1367741936@facebook.com";
                        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 = "100004765716168@facebook.com";
                                id = 100004765716168;
                                name = "AccountDevelopment Development";
                            ,
                                                        
                                email = "1367741936@facebook.com";
                                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 = "1367741936@facebook.com";
                    id = 1367741936;
                    name = " xxx xxx";
                ,
                                
                    email = "100004765716168@facebook.com";
                    id = 100004765716168;
                    name = "AccountDevelopment Development";
                
            );
        ;
        senders =         
            data =             (
                                
                    email = "1367741936@facebook.com";
                    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";
    
)

【问题讨论】:

我已经编辑了我的帖子。谢谢。 【参考方案1】:

您的访问器中缺少一个级别。 JSON 中的***项目是messages,其中包含data。然后data 是一个数组,而不是字典。所以你应该把第一行改成这样……

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

【讨论】:

感谢您的回答,但它不起作用。这就是我得到的:code 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] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x335332a3 0x3a21597f 0x3347eb75 0xdb9cd 0x3a109d91 0x3b56311f 0x3b566ecf 0x3b566dc1 0x3b56791d 0x3b567ac1 0x3a18ba11 0x3a18b8a4) libc++abi.dylib: terminate called throwing an exception messages 是否包含有效对象。可以记录吗? 2012-12-05 10:24:20.372 WebView[191:6a03] messages (null) 对不起,我以为你在记录 json 对象。我想我现在已经修复了代码 对不起,没有。我收到此错误2012-12-05 18:40:58.182 WebView[1418:8a63] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1eda56b0 2012-12-05 18:40:58.184 WebView[1418:8a63] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x1eda56b0' *** First throw call stack: (0x334532a3 0x3a13597f 0x33456e07 0x33455531 0x333acf68 0x68d73 0x3a029d91 0x3b48311f 0x3b486ecf 0x3b486dc1 0x3b48791d 0x3b487ac1 0x3a0aba11 0x3a0ab8a4) libc++abi.dylib: terminate called throwing an exception

以上是关于解析 JSON Facebook 对话 XCODE的主要内容,如果未能解决你的问题,请参考以下文章

PHP - 解析 Facebook 帖子 JSON

Facebook API响应Xcode Parse [重复]

在xcode中访问JSON对象[重复]

解析 Facebook Dialog Delegate 方法未被调用

如何从 swift 3 facebook 图形请求中解析这个 JSON

XCode - Swift - 使用 Parse 进行 Facebook 身份验证