访问由 NSJSONSerialization 生成的 NSDictionary 内的 JSON 数据

发布于 2024-12-22 10:10:49 字数 1345 浏览 2 评论 0原文

访问以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-12-29 10:10:49

问题出在你的for循环中

for(NSDictionary *item in [jsonData objectForKey:@"page"]) 

你不会在item中获得NSDictionary,它会返回Dictionary的密钥,这将是NSString

检查此链接for Objective c 中的每个循环用于访问 NSMutable 字典 来了解如何遍历 NSDictionay

下面是修改后的代码根据您的要求,可能会帮助您

if (!jsonData) {
        NSLog(@"Error parsing JSON: %@", e); } else {
        NSArray *attachments = [[jsonData objectForKey:@"page"] objectForKey:@"attachments"];
        for(NSDictionary *object in attachments) {
              NSLog(@"%@", [object objectForKey:@"images"]);
              NSLog(@"%@", [[[object objectForKey:@"images"] objectForKey:@"full"] objectForKey:@"url"]);
        } 
}

The problem is in you for loop

for(NSDictionary *item in [jsonData objectForKey:@"page"]) 

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

if (!jsonData) {
        NSLog(@"Error parsing JSON: %@", e); } else {
        NSArray *attachments = [[jsonData objectForKey:@"page"] objectForKey:@"attachments"];
        for(NSDictionary *object in attachments) {
              NSLog(@"%@", [object objectForKey:@"images"]);
              NSLog(@"%@", [[[object objectForKey:@"images"] objectForKey:@"full"] objectForKey:@"url"]);
        } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文