0

我需要你的帮助。我可以正确解析 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";
    }
)
4

1 回答 1

0

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

NSArray *messageGroups = [json objectForKey:@"data"];
NSDictionary* firstMessageGroup = [messageGroups objectAtIndex:0];
NSDictionary *messages = [firstMessageGroup objectForKey:@"messages"];
NSArray* items = [messages objectForKey@"data"];
于 2012-12-04T17:13:44.067 回答