解析检索到的Facebook好友详细信息数据

时间:2013-04-04 05:56:49

标签: iphone facebook fetch friend details

我使用FQL查询一次检索好友列表:

-(void)fetchSaveUserFriendDetails
{
 NSString* query = [NSString stringWithFormat:@"SELECT uid,name,birthday_date FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"];

 // Set up the query parameter
  NSDictionary *queryParam = [NSDictionary dictionaryWithObjectsAndKeys:
                                    query, @"q", nil];
 // Make the API request that uses FQL
  [FBRequestConnection startWithGraphPath:@"/fql"
                                     parameters:queryParam
                                     HTTPMethod:@"GET"
                              completionHandler:^(FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error) {
    if (!error)
    {
       NSLog(@"result is %@",result);

       NSArray *resultData = [result objectForKey:@"data"];
       if ([resultData count] > 0) {
       for (NSUInteger i=0; i<[resultData count] ; i++) {
          [self.friendsDetailsArray addObject:[resultData objectAtIndex:i]];
             NSLog(@"friend details are %@",friendsDetailsArray);
     }
   }
 }

        }];
 //Save friends to database stuff
 ..................
}

我得到以下输出:

enter image description here

我已经阅读了Facebook Developers FQL查询的官方文档,但我无法找到如何解析数据。

我尝试了以下方法来解析检索到的数据:

NSString *jsonString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSDictionary *jsonValue = [jsonString JSONValue];
NSArray *values = [jsonValue objectForKey:@"friendinfo"];

其中result以JSON格式保存所有朋友数据,但我收到以下错误:

-JSONValue failed. Error is: Unexpected end of input

UPDATE-INFO

我正在尝试解析所有检索到的数据(最终是JSON格式)并将所有Facebook好友详细信息保存到我的数据库中。所有检索和保存到数据库部分的实现都是在Facebook同步按钮操作中完成的。

需要一些帮助,任何帮助都非常鼓掌,谢谢:)

2 个答案:

答案 0 :(得分:5)

<强>更新

- (void)request:(FBRequest *)request didLoad:(id)result {
        if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
            result = [result objectAtIndex:0];
        }
        friends = [[NSMutableArray alloc] initWithCapacity:1];
        NSArray *resultData = [result objectForKey:@"data"];
        if ([resultData count] > 0) {
            for (NSUInteger i=0; i<[resultData count] && i < 25; i++) {
                [friends addObject:[resultData objectAtIndex:i]];
            }
            [friends retain];
            [tblInviteFriends reloadData];              
        } else {
                UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"No Friends" message:@"You have no friends." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [alt show];
                [alt release];
        }
}

并在cellForRowAtIndexPath中显示数据,如下所示......

cell.textLabel.text = [[friends objectAtIndex:indexPath.row] objectForKey:@"name"]

yourDataArray定义为NSMutableArray并在UITableView中将其用于数据显示

我希望这对你有用... ...

答案 1 :(得分:0)

我不确定您是指具体使用SQLite还是Core Data,但无论如何,您需要以某种方式序列化此数据。有很多种方法和方法。您可以使用的库。我个人最喜欢的是使用Mantle作为我自己模特的基线。

所以,假设我需要使用我得到的查询响应创建一个用户对象。

[FBRequestConnection startWithGraphPath:@"/fql"
                             parameters:queryParam
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
                          if (!error) {
                              NSMutableArray *users = [NSMutableArray array];
                              for (NSDictionary *userInfo in result) {
                                  User *user = [User modelWithRemoteDictionary:userInfo];
                                  [users addObject:user];
                              }
                          }
                      }];

好的,所以要打破这个。您正在对结果对象使用快速枚举来单独获取每个字典。然后,您将该字典序列化为您为其创建模型的User对象。 Mantle提供modelWithRemoteDictionary:,它实际上只是将字典序列化为您在User模型中预设为私有方法的键,然后将其设置为同一User的公共属性模型。最后,将每个对象添加到数组中。

你的最终产品是那个数组,你可以用来做你想做的任何事情。

我希望这有帮助!

修改

跳过模型:

[FBRequestConnection startWithGraphPath:@"/fql"
                             parameters:queryParam
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
                          if (!error) {
                              self.users = [NSMutableArray array];
                              for (NSDictionary *userInfo in result) {
                                  [self.users addObject:userInfo];
                              }
                          }
                      }];

为了举例说明,让我们使用cellForRowAtIndexPath:显示稍后如何使用此数组:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Allocate, and set the cell...

    NSDictionary *userDict = self.users[indexPath.row];

    cell.name = userDict[@"facebook_name"];
    cell.username = userDict[@"facebook_username"];
    cell.birthday = userDict[@"facebook_birthday"];
}