twitterの検索を行い、 結果のJSONをparseする(NSJSONSerialization)
Objective-Cでのtwitter検索とJSONの取り扱いについて調べていたら、ちょうどいい例があったので紹介。
Getting Started with JSON in iOS5 | CapTech Consulting Blogs
iOS5から導入された NSJSONSerialization を使うと、JSONのparseが行えます。JSONの外部ライブラリを使わなくてもいいってことですかねー。
あと、例だと NSInputStream で検索結果を取得し、[NSJSONSerialization JSONObjectWithStream]に入れてます。これ以外にも、NSURLConnectionでデータを取得して[NSJSONSerialization JSONObjectWithData]を使うのもありかなと。
あと、上記サイトのソースは動かなかった(SearchのURLが違っていた)ので書きなおしてみました。
NSData *tweets = [NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://search.twitter.com/search.json?q=from:tototti"]]; NSInputStream *twitterStream = [[NSInputStream alloc] initWithData:tweets]; [twitterStream open]; if (twitterStream) { NSError *parseError = nil; id jsonObject = [NSJSONSerialization JSONObjectWithStream:twitterStream options:NSJSONReadingAllowFragments error:&parseError]; if ([jsonObject respondsToSelector:@selector(objectForKey:)]) { for (NSDictionary *tweet in [jsonObject objectForKey:@"results"]) { NSArray* allkeys = [tweet allKeys]; for (NSString* key in allkeys){ NSLog(@"%@: %@", key, [tweet objectForKey:key]); } } } } else { NSLog(@"Failed to open stream."); }