从 NSDictionary 获取对象

发布于 2024-12-08 19:11:59 字数 737 浏览 0 评论 0原文

我从 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 技术交流群。

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

发布评论

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

评论(4

不再见 2024-12-15 19:12:01

您拥有的是一个 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.

绿萝 2024-12-15 19:12:01

您将无法直接从 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.

╭⌒浅淡时光〆 2024-12-15 19:12:01

@MatthewGillingham 建议使用 JSONKit。我想它做得很好,但我一直使用它的竞争对手json 框架。没有真正的原因,我只是先发现它并先了解它。我确实认为它的界面更简单,但很多人也使用 JSONKit 做得很好。

使用 json-framework:

require JSON.h

...然后

NSString *myJsonString = @"[{'whatever': 'this contains'}, {'whatever', 'more content'}]";

NSArray *data = [myJsonString JSONValue];

foreach (NSDictionary *item in data) {
    NSString *val = [item objectForKey:@"whatever"];
    //val will contain "this contains" on the first time through
    //this loop, then "more content" the second time.
}

@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:

require JSON.h

...and then

NSString *myJsonString = @"[{'whatever': 'this contains'}, {'whatever', 'more content'}]";

NSArray *data = [myJsonString JSONValue];

foreach (NSDictionary *item in data) {
    NSString *val = [item objectForKey:@"whatever"];
    //val will contain "this contains" on the first time through
    //this loop, then "more content" the second time.
}
筱武穆 2024-12-15 19:12:01

如果您有字典数组,只需将数组中的对象分配给字典

NSDictionary *dictionary = [array objectAtIndes:0];

,然后使用该字典来获取值。

If you have array of dictionary just assign objects in array to dictionary like

NSDictionary *dictionary = [array objectAtIndes:0];

and then use this dictionary to get values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文