ios5 - JSON解析 - 字典OK,不解析为数组

时间:2012-03-19 14:35:41

标签: json ios5 nsjsonserialization

我正在使用新的ios5“NSJSONSerialization”功能从网站获取一些技术新闻。我似乎一切正常,直到我尝试将NSDictionary解析为NSArray。我在这里包含了代码。第一个控制台测试(测试1)工作得很好 - 我得到一个充满返回的json数据的控制台。但是在第二个控制台测试(测试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]);

0 个答案:

没有答案