我正在尝试从简单的json响应中获取数据。
第一个日志显示我找到一个名为access_token的项目,但是我不能使用它的objectForKey,我得到[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72de3b0
我做错了什么?
for(NSDictionary *item in authResponse)
{
NSString *s1 = @"access_token";
NSString *s2 = [item description];
NSLog(@"access desc: %@", [item description]);
if([s1 isEqualToString:s2]){
NSLog(@"Item: %@", [item objectForKey:@"access_token"]);
}
}
答案 0 :(得分:1)
authResponse
对象是NSDictionary
对象,但它中将包含许多其他类型,包括其他NSDictionary
个对象。您需要更加谨慎地解释每个成员的解释:
for (NSObject *item in authResponse)
{
if ([item isKindOfClass:[NSDictionary class]])
{
NSDictionary *dictItem = (NSDictionary *)item;
// do thing with sub-dictionary
}
else if ([item isKindOfClass:[NSString class]])
{
NSString *stringItem = (NSString *)item;
// do thing with string item
}
else if ([item isKindOfClass:[NSNumber class]])
{
NSNumber *numberItem = (NSNumber *)item;
// do thing with number item
}
}