objective c - Manipulating Facebook/JSON data -
i'm trying handle facebook json data , transform nsmutable dictionary, i'm getting (null) when try print data. although when try count, number.
user_likes nsmutabledictionary globally defined. i'm getting (null) on line:
nslog(@"user likes: %@", user_likes);
this code:
nsstring *query = @"select page_id, type page_fan uid = me() "; // set query parameter nsdictionary *queryparam = @{ @"q": query }; // make api request 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 print results, lot of data facebook, 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"; },
you can't directly
user_likes = [nsjsonserialization jsonobjectwithdata:results options:kniloptions error:&error];
you need first create dictionary data in results
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 class project , import in class 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 can you.
Comments
Post a Comment