ios - 从 JSON 从 NSDictionary 中获取键值对

标签 ios objective-c json nsdictionary for-in-loop

我对这一切有点陌生,我无法从传入的 JSON NSDistionary 中获取信息。这是字典:

enter image description here

作为第一步,我尝试检索地址部分的邮政编码,这是代码:

               for (NSString *address in jsonData [@"address"][@"postcode"]) {
                         NSLog(@"address is ===> %@", address);
                   }

然而,当我运行它时,我收到了一个 NSException 错误: 'NSInvalidArgumentException',原因:'-[__NSArrayM objectForKeyedSubscript:]:无法识别的选择器发送到实例 0x79e4daf0'

如有任何帮助,我们将不胜感激。 谢谢

最佳答案

如图所示,您的 JSON 文件包含 3 个对象的数组,我们首先将数据转换为数组。 然后你必须通过使用访问字典

1.valueForKeyPath@"dictionarykey1.dictionarykey2"。

2.objectForKey@“dictionarykey1”。

NSLog(@"JSON: %@", responseObject);

    NSArray *Response=(NSArray*)[responseObject copy];

    NSString * Address=[Response[0] valueForKeyPath:@"address.postcode"];

关于ios - 从 JSON 从 NSDictionary 中获取键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292669/

相关文章:

objective-c - 等待 RestKit 对象管理器完成初始化

php - FullCalendar JSON Feed 不起作用 : my calendar doesn't put the data from the JSON feed onto my webpage

javascript - Wordpress API,React : this. state.project.title 未定义

json - Unix jq 解析通配符

objective-c - 为什么需要 [super loadView] 或 [super viewDidLoad]?

ios - missViewController 未调用完成处理程序

objective-c - 根据来自另一个NSArray的字符串对NSMutableArray进行排序

ios - 实例被释放,而键值观察者仍然在其中注册

ios - 从另一个 View Controller 访问更新的变量值

ios - 使用 Swift 3 在 UserDefaults 中注册多个值类型