man*_*urt 3 json dictionary key ios
我正在导入JSON字典.我需要知道使用它的键的名称.
字典正在加载数据确定:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *results = [responseString JSONValue];
NSLog(@"tenga: %@",results);
Run Code Online (Sandbox Code Playgroud)
但当我尝试获取键的名称时,应用程序崩溃:
NSArray * keys = [results allKeys];
NSLog(@"keys: %@",keys); ...}
Run Code Online (Sandbox Code Playgroud)
错误信息:
[__NSArrayM allKeys]:无法识别的选择发送到实例0x5a16b30 2011-08-30 22:52:26.171 Twitter的搜索[1906:207] *终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:" - [__ NSArrayM allKeys]:无法识别选择发送到实例0x5a16b30'
为什么allKeys不工作?
如何获取密钥的名称,以便开始处理对象?
编辑
我正在使用 http://code.google.com/p/json-framework Stig Brautaset json框架
您从JSON字符串获取的URL为您提供了一个数组,而不是一个对象,即它看起来像:
[ { "foo1" : "bar1" }, { "foo2" : "bar2" },... ]
Run Code Online (Sandbox Code Playgroud)
注意括号[ ].在这种情况下,您的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.
}
Run Code Online (Sandbox Code Playgroud)