访问由 NSJSONSerialization 生成的 NSDictionary 内的 JSON 数据
访问以下 URL 中的 JSON 数据时遇到问题 ( http://jamesstenson.com/portraits/?json =1 ),基本上我想访问“附件”下面的“完整”“url”。我目前的代码如下:
NSError *e = nil;
NSData *jsonFeed = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://jamesstenson.com/portraits/?json=1"]];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:jsonFeed options:NSJSONReadingMutableContainers error: &e];
if (!jsonData) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in [jsonData objectForKey:@"page"]) {
for(NSDictionary *attachment in [item objectForKey:@"images"]) {
NSLog(@"%@", attachment);
}
}
}
这不断抛出错误:
2011-12-21 10:13:39.362 JSON[3463:f803] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a7b500
2011-12-21 10:13:39.363 JSON[3463:f803] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFString objectForKey:]:无法识别的选择器发送到实例0x6a7b500'
我知道我错误地访问了这些项目,但不知道如何实现这一点。我尝试了多种解决方案,例如 http://blogs.captechconsulting .com/blog/nathan-jones/getting-started-json-ios5 - 但没有运气。我是 iOS 开发的新手,对 JSON 有一点了解。提前感谢大家的帮助。
Having some trouble accessing the JSON data in the following URL ( http://jamesstenson.com/portraits/?json=1 ), basically I want to access the "full" "url"'s underneath "attachments". My code at the moment is as follows:
NSError *e = nil;
NSData *jsonFeed = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://jamesstenson.com/portraits/?json=1"]];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:jsonFeed options:NSJSONReadingMutableContainers error: &e];
if (!jsonData) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in [jsonData objectForKey:@"page"]) {
for(NSDictionary *attachment in [item objectForKey:@"images"]) {
NSLog(@"%@", attachment);
}
}
}
This keeps throwing up an error:
2011-12-21 10:13:39.362 JSON[3463:f803] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a7b500
2011-12-21 10:13:39.363 JSON[3463:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a7b500'
I am aware I am accessing the items wrongly, but cannot figure out how to achieve this. I've tried several solutions such as http://blogs.captechconsulting.com/blog/nathan-jones/getting-started-json-ios5 - but no luck. I am complete newbie to iOS development and have a little knowledge of JSON. Thanks for everyones help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在你的for循环中
你不会在item中获得NSDictionary,它会返回Dictionary的密钥,这将是NSString
检查此链接for Objective c 中的每个循环用于访问 NSMutable 字典 来了解如何遍历 NSDictionay
下面是修改后的代码根据您的要求,可能会帮助您
The problem is in you for loop
You won't get NSDictionary in item, it will return you key of Dictionary, which would be NSString
Check this link for each loop in objective c for accessing NSMutable dictionary to know how to traverse through NSDictionay
Below is the modified code for your requirement, might help you