JSON 字典中的键

发布于 2024-12-02 05:59:33 字数 1055 浏览 2 评论 0原文

我正在导入 JSON 字典。我需要知道使用它的按键名称。

字典加载数据正常:

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];    
  NSDictionary *results = [responseString JSONValue];
  NSLog(@"tenga: %@",results);

但是当我尝试获取键的名称时,应用程序崩溃:

NSArray * keys = [results allKeys];
NSLog(@"keys: %@",keys); ...}

错误消息:

[__NSArrayM allKeys]:无法识别的选择器发送到实例 0x5a16b30 2011-08-30 22:52:26.171 Twitter Search[1906:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayM allKeys]:无法识别的选择器发送到实例 0x5a16b30 '

为什么 allKeys 不起作用?

如何获取键的名称以便我可以开始使用对象?

编辑

我使用 http://code.google.com/p/json-framework Stig Brautaset json 框架

I'm importing a JSON dictionary. I need to know the name of the keys to work with it.

The dictionary is loading the data ok:

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];    
  NSDictionary *results = [responseString JSONValue];
  NSLog(@"tenga: %@",results);

but when I try to get the names of the keys the app crashes:

NSArray * keys = [results allKeys];
NSLog(@"keys: %@",keys); ...}

error message:

[__NSArrayM allKeys]: unrecognized selector sent to instance 0x5a16b30
2011-08-30 22:52:26.171 Twitter Search[1906:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM allKeys]: unrecognized selector sent to instance 0x5a16b30'

Why is allKeys not working?

How do I get the names for my keys so I can start working with the objects?

edit

Im using the http://code.google.com/p/json-framework Stig Brautaset json framework

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜味超标? 2024-12-09 05:59:33

您从中获取 JSON 字符串的 URL 为您提供了一个数组,而不是一个对象,即它看起来类似于:

[ { "foo1" : "bar1" }, { "foo2" : "bar2" },... ]

请注意括号 [ ]。在这种情况下,您的 JSON 解析器会为您提供一个 NSArray 作为顶级 (Objective-C) 对象。您需要一些逻辑,例如:

id results = [responseString JSONValue];
if ([results isKindOfClass: [NSArray class]])
{
    // probably iterate through whtever is in it
}
else if ([results isKindOfClass: [NSDictionary class]])
{
    // dictionary at the top level.  Hooray!
}
else
{
    // something went horribly wrong, deal with it.
}

The URL that you obtained that JSON string from gave you an array, not an object i.e. it looked something like:

[ { "foo1" : "bar1" }, { "foo2" : "bar2" },... ]

Note the brackets [ ]. In that situation, your JSON parser gave you an NSArray as the top level (Objective-C) object. You need some logic like:

id results = [responseString JSONValue];
if ([results isKindOfClass: [NSArray class]])
{
    // probably iterate through whtever is in it
}
else if ([results isKindOfClass: [NSDictionary class]])
{
    // dictionary at the top level.  Hooray!
}
else
{
    // something went horribly wrong, deal with it.
}
酒绊 2024-12-09 05:59:33

这里的结果不是 NSDictionary 而是 NSArray。 NSArray 没有导致崩溃的 allKeys 选择器。如果您想发布有关您正在使用的 JSON 框架的更多信息,我们可以帮助更好地查找问题根源

What you have here is the results is not an NSDictionary but rather a NSArray. NSArray doesn't have the allKeys selector causing the crash. If you'd post more info on the JSON Framework you are using, we could help source the issue better

暖树树初阳… 2024-12-09 05:59:33

发布的日志显示此调用 [responseString JSONValue] 返回 NSArray

Posted log says that this call [responseString JSONValue] returns NSArray.

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