简体   繁体   English

通过JSON从NSDictionary中获取键值对

[英]getting key value pairs out of NSDictionary from JSON

I'm a little new to all this and I'm having trouble getting information out of an incoming JSON NSDistionary. 我对这一切还有些陌生,并且在从传入的JSON NSDistionary中获取信息时遇到了麻烦。 Heres the dictionary: 这是字典:

在此处输入图片说明

As a first step I'm trying to retrieve the address sections postcode, here's the code: 首先,我尝试检索地址部分的邮政编码,这是代码:

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

However when I run this i am receiving an NSException error: 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x79e4daf0' 但是,当我运行此程序时,我收到一个NSException错误:'NSInvalidArgumentException',原因:'-[__ NSArrayM objectForKeyedSubscript:]:无法识别的选择器发送到实例0x79e4daf0'

Any help with this would be very much appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

As Shown your JSON file contain an array of 3 objects, we first convert data to array. 如图所示,您的JSON文件包含3个对象的数组,我们首先将数据转换为数组。 Then you have to access dictionary by using 然后,您必须使用来访问字典

1.valueForKeyPath@"dictionarykey1.dictionarykey2". 1.valueForKeyPath @ “dictionarykey1.dictionarykey2”。

2.objectForKey@"dictionarykey1". 2.objectForKey @ “dictionarykey1”。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM