如何将从多查询FQL接收的结果读入目标c中的字符串?

时间:2013-10-08 04:26:21

标签: iphone objective-c facebook-fql fql.multiquery

我希望能够从我的多查询结果中提取数据。

我正在使用此代码进行多次查询:

NSString *query =
@"{"
@"'friends':'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25',"
@"'friendinfo':'SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM #friends)',"
@"}";

NSDictionary *queryParam = @{ @"q": query };

[FBRequestConnection startWithGraphPath:@"/fql"
                             parameters:queryParam
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
                          if (error) {
                              NSLog(@"Error: %@", [error localizedDescription]);
                          } else {
                              NSLog(@"Result: %@", result);
                          }
                      }];

然后我将结果打印到命令行,我看到了我想看到的所有数据。

Result: {
data =     (
            {
        "fql_result_set" =             (
                            {
                uid2 = 12345;
            },
                            {
                uid2 = 12345;
            }
        );
        name = friends;
    },
            {
        "fql_result_set" =             (
                            {
                name = "gg";
                "pic_big" = "https://fbcdn-profile-a.akamaihd.net/gg";
                sex = gg;
                uid = 12345;
            },

                            {
                name = "gg";
                "pic_big" = "https://fbcdn-profile-a.akamaihd.net/gg";
                sex = gg;
                uid = 12345;
            },
                            {
                name = "gg";
                "pic_big" = "https://fbcdn-profile-a.akamaihd.net/gg";
                sex = gg;
                uid = 12345;
            }
        );
        name = friendinfo;
    }
);
}

我无法弄清楚如何从结果中的每个条目中提取名称,图片网址,性别和uid。我觉得我需要使用一些数组和字典的组合,但我不确定如何。在此先感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:0)

有点晚了但是,这就是我为其他任何一个人做的事情......

//get data into array
NSArray *data = [result objectForKey:@"data"];

//loop queries in array
for (NSArray *nextQuery in data) {

    //debug query name (use this for if statement to handle query data)       
    NSLog(@"query name - %@", [nextQuery valueForKeyPath:@"name"]);

    //get query data
    NSArray *queryData = [nextQuery valueForKeyPath:@"fql_result_set"];

    //
    NSLog(@"uid %@", [queryData valueForKeyPath:@"uid"])

}

您可以使用键路径和点运算符的值来获取嵌套对象。

希望这有帮助。