新しいios5「NSJSONSerialization」機能を使用して、Webサイトからいくつかの技術ニュースを入手しています。NSDictionaryをNSArrayに解析しようとする時点まで、すべてが正常に機能しているようです。ここにコードを含めました。最初のコンソールテスト(テスト1)はうまく機能しています-返されたjsonデータでいっぱいのコンソールを取得します。ただし、2番目のコンソールテスト(テスト2-nsarrayの内容をチェックするテスト)では、「配列が返されました:(null)」というメッセージが表示されます。何か案は?エラー変数を再利用したからでしょうか?私は困惑しています。
コードは次のとおりです。
// create the string for making the api call to the website
NSString* theURL = [NSString stringWithFormat:@"http://pipes.yahoo.com/pipes/pipe.run?_id=9DfJELfJ2xGnjAQWJphxuA&_render=json"];
// declare and assign variables necessary to generate the actual url request
NSError* err = nil;
NSURLResponse* response = nil;
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
NSURL* URL = [NSURL URLWithString:theURL];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
// make the actual url request - get the data from yahoo pipes, in json format
NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
// assign the entire result to a dictionary
NSDictionary *resultsDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&err];
//*** test 1: use the console to test results:
NSLog(@"NSDictionary returned: %@",resultsDictionary);
// parse the whole returned list of items (in dictionary form) into a large array
NSArray* arrayOfReturnedItems = [resultsDictionary objectForKey:@"items"];
//*** test 2: view the items returned (in the console)
NSLog(@"Array returned: %@",[arrayOfReturnedItems objectAtIndex:0]);