如何管理Facebook api响应数据

时间:2011-05-25 19:53:25

标签: iphone facebook api

我正在开发一款适用于iPhone的应用程序,我将此网址发送到Facebook: https://graph.facebook.com/search?type=checkin&access_token=ACCESS_TOKEN

并且facebook回复了这样的话:

{
   "data": [
      {
         "id": "2081746282701",
         "from": {
            "name": "USER1",
            "id": "xxxxxxxxxx"
         },
         "place": {
            "id": "xxxxxxxxx",
            "name": "\u03a0\u03b1\u03bb\u03bb\u03ac\u03b4\u03b9\u03bf",
            "location": {
               "city": "xxxxxxxx",
               "country": "Greece",
               "latitude": xxxxx,
               "longitude":xxxxx
            }
         },
         "application": {
            "name": "Facebook for iPhone",
            "id": "6628568379"
         },
         "created_time": "2011-05-25T01:22:00+0000",
         "likes": {
            "data": [
               {
                  "id":"xxxxxx",
                  "name": TaggedUsers"
               },
               {
                  "id": "xxxxxx",
                  "name": "tagged user"
               }
            ]
         }
      },
      {
         "id": "xxxxxxx",
         "from": {
            "name": "UserWhich puplish this",
            "id": "740646984"
         },
         "tags": {
            "data": [
               {
                  "name": "XXXXXX",
                  "id": "xxxxxxxxxx"
               }
            ]
         },
         "place": {
            "id": "xxxxxxxxxxxxx",
            "name": "xxxxxxxx",
            "location": {
               "latitude": xxxxxx,
               "longitude": xxxxxx
            }
         },
         "application": {
            "name": "Facebook for Android",
            "id": "350685531728"
         },
         "created_time": "2011-05-23T17:18:54+0000",
         "likes": {
            "data": [
               {
                  "id": "xxxxxxx",
                  "name": "xxxxxxxxx"
               }
            ]
         }
      }
}

我的问题是我将如何管理此字符串以导出长拉特位置和签入的用户?我认为NSCaseInsensitiveSearch不会这样做,因为没有唯一的变量可供搜索。例如,“数据”放置在响应字符串的开头,每次签入之前,但放置在标记用户之前!

1 个答案:

答案 0 :(得分:2)

这是JSON:JavaScript Object Notation。

您可以使用json-framework轻松将其解码为NSDictionary或NSArray,具体取决于根元素。之后,它是一个树状结构,具体取决于返回的JSON。

提到的框架为NSObject提供了一个易于使用的类别,它提供了JSONValue方法。

解码收到的json的示例:

NSString *myJson = @"...";
NSDictionary *decodedJson = [myJson JSONValue];

现在,您可以使用快速枚举或直接访问(objectForKey:)来检索您感兴趣的数据。

NSArray *users = [decodedJson objectForKey:@"data"];

foreach(NSDictionary *user in users =) {
    NSLog(@"user's id: %@", [user objectForKey:@"id"]);
    NSDictionary *fromData = [user objectForKey:@"from"]);
    NSLog(@"user is from: %@", [fromData objectForKey:@"name"]);
    NSLog(@"user is from id: %@", [fromData objectForKey:@"id"]);
    // and so on ..
}