Reputation: 43
I want to get tweets with a specific hashtag. I use the Twitter search URL. This is my code:
NSMutableString *urlString = [NSMutableString stringWithFormat:@"%s","http://search.twitter.com/search.json?q=%23zesdaagsegent"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"%@", data);
NSError *error;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@", json);
NSMutableArray *results =[NSMutableArray array];
for(NSDictionary *item in json)
{
[results addObject:[item objectForKey:@"results"]];
}
My NSLog gets me the output I need:
2013-05-28 09:48:45.080 ZesdaagseGent[572:11303] { "completed_in" = "0.023"; "max_id" = 339031661368975360; "max_id_str" = 339031661368975360; page = 1; query = "%23zesdaagsegent"; "refresh_url" = "?since_id=339031661368975360&q=%23zesdaagsegent"; results = ( { "created_at" = "Mon, 27 May 2013 14:53:41 +0000"; "from_user" = SigfridMaenhout; "from_user_id" = 369194526; "from_user_id_str" = 369194526; "from_user_name" = "Sigfrid Maenhout"; geo = ""; id = 339031661368975360; "id_str" = 339031661368975360; "iso_language_code" = nl; metadata = { "result_type" = recent; }; source = "<a href="http://twitter.com/">web</a>"; text = "#zesdaagsegent Het is een zonnige dag hier in Merelbeke. We verlangen allemaal naar een beetje zonnestralen in het gezicht, toch?"; } ); "results_per_page" = 15; "since_id" = 0; "since_id_str" = 0; }
I need the array "results" with the objects in. My problem is that I can't get the results in an NSMutableArray with the method objectForKey.
Does anyone has an idea?
Upvotes: 2
Views: 192
Reputation: 1320
Try this :
NSMutableArray *results = [NSMutableArray array];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
for (NSDictionary *result in jsonResponse[@"results"]) {
[results addObject:result];
}
And you should have a NSArray results
containing X NSDictionary for each of the matching tweets.
PS : [@"results"] is the modern Objective-C syntax for [NSDictionary objectForKey:@"results"]
Upvotes: 2