iphone - Objective-C JSON 解析错误

标签 iphone ios json parsing

我正在尝试解析这个 JSON 数组:

http://www.ifanatic.hu/api/get_recent_posts/?dev=1

但是我得到这个错误:

    2012-07-15 22:31:59.038 JSONTest[610:f803] -[__NSCFDictionary indexOfObject:]: unrecognized selector sent to instance 0x6a92850
2012-07-15 22:31:59.040 JSONTest[610:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary indexOfObject:]: unrecognized selector sent to instance 0x6a92850'
*** First throw call stack:
(0x13d7052 0x1568d0a 0x13d8ced 0x133df00 0x133dce2 0x236b 0xa23a59 0xa21e94 0xa22eb7 0xa21e4f 0xa21fd5 0x966f6a 0x3989bbd 0x3a565ea 0x3980298 0x3a5616b 0x3980137 0x13ab97f 0x130eb73 0x130e454 0x130ddb4 0x130dccb 0x12c0879 0x12c093e 0x2ea9b 0x1cb8 0x1c15)
terminate called throwing an exception

这是我的来源:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSArray* latestPosts = [responseString JSONValue];

NSDictionary* post = [latestPosts objectAtIndex:[latestPosts indexOfObject:0]];

NSString* title = [post objectForKey:@"title"];

label.text = [NSString stringWithFormat:@"Title: %@", title];

最佳答案

在解析 JSON 时,JSON 对象的根元素被转换为 NSDictionary。并且 NSDictionary 不响应

indexOfObject:

消息。你需要做的是使用:

NSDictionary *post = [[latestPosts objectForKey:@"posts"] objectAtIndex:0];

等等

关于iphone - Objective-C JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11495458/

相关文章:

ios - 如何根据设备大小和比例调整 UIImage 的大小?

iOS 辅助功能提示未在文本字段上说出

php - 如何使用 jQuery 访问 JSON 的多级对象

java - 404错误的解决方法 使用带有json的restful web services

iphone - 如何在不显示对话框的情况下发布到 Facebook?

iphone - iPhone SDK 的文件错误

ios - 原型(prototype)单元中的约束

iphone - 如何向字符串添加版权符号?

ios - 随机词生成器-IOS开发

json - 使用 R 将 xlsx 转换为 json