操纵Facebook / JSON数据

时间:2014-04-08 15:29:34

标签: objective-c json facebook-graph-api

我试图处理Facebook JSON数据并将其转换为NSMutable词典,但是当我尝试打印数据时,我得到(null)。虽然当我尝试计算时,我会得到一个数字。

User_likes是全局定义的NSMutableDictionary。我在这一行得到(null):

NSLog(@"User likes: %@", user_likes);

这是我的代码:

NSString *query =
 @"SELECT page_id, type FROM page_fan WHERE uid = me() ";
 // Set up the query parameter
 NSDictionary *queryParam = @{ @"q": query };
 // Make the API request that uses FQL
 [FBRequestConnection startWithGraphPath:@"/fql"
 parameters:queryParam
 HTTPMethod:@"GET"
 completionHandler:^(FBRequestConnection *connection,
 id results,
 NSError *error) {
 if (error) {
 NSLog(@"Error: %@", [error localizedDescription]);
 } else {

 user_likes = [NSJSONSerialization JSONObjectWithData:results options:kNilOptions error:&error];
 NSLog(@"User likes: %@", user_likes);

 NSInteger* n_user_likes = [results count];
 NSInteger* n_user_likes2 = [user_likes count];

 NSLog(@"n user likes %qi", n_user_likes);
 NSLog(@"n user likes2 %qi", n_user_likes2);

 id val = nil;
 id values = [[user_likes allKeys] objectAtIndex:0 ];
 NSLog(@"values id %@", values);

当我打印结果时,我从Facebook获得了大量数据,这是一个样本:

data =     (
                {
            "page_id" = 253370381511811;
            type = "PUBLIC FIGURE";
        },
                {
            "page_id" = 148389618201;
            type = "LOCAL BUSINESS";
        },
                {
            "page_id" = 213631462169238;
            type = COMMUNITY;
        },
                {
            "page_id" = 162297750451425;
            type = "NON-PROFIT ORGANIZATION";
        },
                {
            "page_id" = 503620106320217;
            type = "MEDIA/NEWS/PUBLISHING";
        },

2 个答案:

答案 0 :(得分:0)

你不能直接做

user_likes = [NSJSONSerialization JSONObjectWithData:results options:kNilOptions error:&error];

您需要首先使用results中的数据创建一个字典,如下所示:

    NSDictionary *dictionary = [NSDictionary dictionaryWithJSONData:results]; 
    user_likes = [NSString stringWithFormat:@"%d",[[dictionary objectForKey:@"value_of_the_likes"] intValue]];
    NSLog(@"%@",user_likes);
    NSLog(@"%@",dictionary);

修改

请为您的项目创建此类,并在需要使用它的类中导入。

#import <Foundation/Foundation.h>

@interface NSDictionary (JSONExtensions)

+(NSDictionary*)dictionaryWithJSONData:(NSData*)data;
-(NSData*)JSONValue;
-(NSString*)JSONString;

@end

@implementation NSDictionary(JSONExtensions)

+(NSDictionary*)dictionaryWithJSONData:(NSData*)data{
    NSError *error = nil;

    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if(error){
        NSLog(@"%@",error);
        return nil;
    };
    return result;
}

-(NSData*)JSONValue{
    NSError *error = nil;

    NSData *result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
    if(error){
        NSLog(@"%@",error);
        return nil;
    };

    return result;
}

-(NSString*)JSONString{
    return [[NSString alloc] initWithData:self.JSONValue encoding:NSUTF8StringEncoding];
}

@end

希望它可以帮到你。

答案 1 :(得分:0)

也许你忘了设置选项:

NSJSONReadingOptions options = NSJSONReadingAllowFragments | NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves;

user_likes = [NSJSONSerialization JSONObjectWithData:results options:options error:&error];

我希望这对你有用!

<强>更新

检查一下: NSJsonSerialzation not parsing results from Facebook - Cocoa error 3840

如果有帮助,请告诉我!