0

I'm trying to handle Facebook JSON data and transform it into a NSMutable Dictionary, but I'm getting (null) when I try to print the data. Although when I try to do a count, I get a number.

User_likes is NSMutableDictionary which is globally defined. I'm getting (null) on this line:

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

This is my code:

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

When I print results, I get a lot of data from Facebook, this is a sample of it:

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";
        },
user3511563
  • 397
  • 2
  • 5
  • 18

2 Answers2

0

you can't do directly

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

you need first create a dictionary with the data in results like this:

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

EDIT:

Please, create this class to your project and import in the class where you need use this.

#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

Hope it can help you.

hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • I'm getting "No known class method for selector 'dictionaryWithJSONData:'" – user3511563 Apr 08 '14 at 16:39
  • Thanks, but now I'm getting "Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0xb949070 {NSDebugDescription=No value.}" when I try to run the line with dictionaryWithJSONData – user3511563 Apr 08 '14 at 17:26
0

maybe you forget set up options:

NSJSONReadingOptions options = NSJSONReadingAllowFragments | NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves;

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

i hope this be useful for you!

UPDATE:

Check this : NSJsonSerialzation not parsing results from Facebook - Cocoa error 3840

Let me know if that helps you!

Community
  • 1
  • 1
albirrojo7
  • 146
  • 4