JSON 字典中的键
我正在导入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您从中获取 JSON 字符串的 URL 为您提供了一个数组,而不是一个对象,即它看起来类似于:
请注意括号
[ ]
。在这种情况下,您的 JSON 解析器会为您提供一个NSArray
作为顶级 (Objective-C) 对象。您需要一些逻辑,例如:The URL that you obtained that JSON string from gave you an array, not an object i.e. it looked something like:
Note the brackets
[ ]
. In that situation, your JSON parser gave you anNSArray
as the top level (Objective-C) object. You need some logic like:这里的结果不是 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
发布的日志显示此调用
[responseString JSONValue]
返回NSArray
。Posted log says that this call
[responseString JSONValue]
returnsNSArray
.