2013-04-20 82 views
-4

我想解析下面的JSON响应:http://www.breakingnews.com/api/v5/items?compact=false阅读JSON结果时出现异常

这里是我的代码来分析它:

NSError* error = nil; 
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.breakingnews.com/api/v5/items?compact=false"] options:NSDataReadingUncached error:&error]; 
if (error) { 
    NSLog(@"%@", [error localizedDescription]); 
} else { 

    NSError *e = nil; 
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error: &e]; 

    if (!jsonArray) { 
     NSLog(@"Error parsing JSON: %@", e); 
    } else { 
     for(NSArray* item in jsonArray) { 

      NSLog(@"Item: %@", item); 

     } 
    } 

} 

不过,我得到这个错误:

-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x101810a40

为什么是这样的 - 我究竟做错了什么?

+0

那个人犯了**完全相同的错误。**还有其他人这样做了。你应该先询问才能搜索。 – 2013-04-20 20:29:05

+0

@ H2CO3,我还没有看到我要去哪里错了? – 2013-04-20 20:32:08

+0

唯一奇怪的是所谓的错误消息 - 我没有看到该代码中的NSString。其实这段代码不会崩溃,它只是显示字典的键。你有没有显示所有相关的代码? – 2013-04-20 20:33:46

回答

2

检查您使用的对象的类型。假设所有东西都是NSArray,实际上JSON中的东西可以是NSArray,NSDictionary,NSString,NSNumberNSNull