2013-04-20 74 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