1

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

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

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;
    }
);
}

我只是不知道如何从结果中的每个条目中提取名称、图片 url、性别和 uid。我觉得我需要使用数组和字典的某种组合,但我不确定如何。提前感谢您的帮助。:)

4

1 回答 1

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"])

}

您可以通过使用键路径和点运算符的值来访问嵌套对象。

希望这可以帮助。

于 2014-01-29T19:57:52.127 回答