从 NSDictionary 获取对象
我从 URL 得到这个结果:
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
它看起来像这样:
[{"modele":"Audi TT Coup\u00e9 2.0 TFSI","modele_annee":null,"annee":"2007","cilindre":"4 cyl","boite":"BVM","transmision":"Traction","carburant":"ES"},
{"modele":"Audi TT Coup\u00e9 2.0 TFSI","modele_annee":null,"annee":"2007","cilindre":"4 cyl","boite":"BVM","transmision":"Traction","carburant":"ES"}]
所以它包含 2 个字典。我需要从这个结果的所有键中获取对象。我该怎么做?
我尝试了这个: NSDictionary vehiculesPossedeDictionary=(NSDictionary *)result;
然后这个: [vehiculesPossedeDictinary objectForKey:@"modele"];
但这不起作用。
请帮助我...提前致谢
I get from an URL this result :
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
it looks like this :
[{"modele":"Audi TT Coup\u00e9 2.0 TFSI","modele_annee":null,"annee":"2007","cilindre":"4 cyl","boite":"BVM","transmision":"Traction","carburant":"ES"},
{"modele":"Audi TT Coup\u00e9 2.0 TFSI","modele_annee":null,"annee":"2007","cilindre":"4 cyl","boite":"BVM","transmision":"Traction","carburant":"ES"}]
So it contains 2 dictionaries. I need to take the objects from all the keys from this result. How can I do this?
I tried this : NSDictionary vehiculesPossedeDictionary=(NSDictionary *)result;
and then this : [vehiculesPossedeDictinary objectForKey:@"modele"];
but this is not working.
Please help me... Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您拥有的是一个 JSON 字符串,它描述了一个包含两个“对象”的“数组”。这需要使用 JSON 解析器转换为 Objective-C 对象,转换后将是一个包含两个 NSDictionary 的 NSArray。
What you have is a JSON string which describes an "array" containing two "objects". This needs to be converted to Objective-C objects using a JSON parser, and when converted will be an NSArray containing two NSDictionarys.
您将无法直接从 JSON 字符串获取字典。您必须首先通过 JSON 解析器运行它。
此时,iOS SDK 中还没有任何构建,因此您必须下载第三方工具并将其包含在您的项目中。
有许多不同的 JSON 解析器,包括 TouchJSON、YAJL 等,您可以找到并比较。就我个人而言,我正在使用 JSONKit。
You aren't going to be able to get your dictionary directly from a string of JSON. You are going to have to going to have to run it through a JSON parser first.
At this point, there is not one build into the iOS SDK, so you will have to download a third-party tool and include it in your project.
There are a number of different JSON parser, include TouchJSON, YAJL, etc. that you can find and compare. Personally, I am using JSONKit.
@MatthewGillingham 建议使用 JSONKit。我想它做得很好,但我一直使用它的竞争对手json 框架。没有真正的原因,我只是先发现它并先了解它。我确实认为它的界面更简单,但很多人也使用 JSONKit 做得很好。
使用 json-framework:
...然后
@MatthewGillingham suggests JSONKit. I imagine it does fine, but I've always used its competitor json-framework. No real reason, I just found it first and learned it first. I do think its interface is somewhat simpler, but plenty of people do fine with JSONKit too.
Using json-framework:
...and then
如果您有字典数组,只需将数组中的对象分配给字典
,然后使用该字典来获取值。
If you have array of dictionary just assign objects in array to dictionary like
and then use this dictionary to get values.