ios5 - Разбор JSON - словарь в порядке, не разбирается в массив - PullRequest
0 голосов
/ 19 марта 2012

Я использую новую функцию ios5 «NSJSONSerialization» для получения технических новостей с веб-сайта.Кажется, у меня все работает нормально, вплоть до того момента, когда я пытаюсь разобрать NSDictionary в NSArray.Я включил код здесь.Первый консольный тест (тест 1) работает отлично - я получаю консоль, полную возвращенных данных json.Но при втором тесте консоли (тест 2 - тот, который проверяет содержимое nsarray), выдает следующее сообщение: «Array return: (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]);
...