iOS检查JSON返回是否有Key

KDM*_*KDM 2 crash ios nsjsonserialization

我发布了一些东西之后,我正在使用NSJsonSerialization来取回一个json返回.我要么得到正确的回报,要么得到:

{
"status":"false"
}
Run Code Online (Sandbox Code Playgroud)

在我尝试解析它之前,我需要检查是否收回了我的回复(因为如果我要求的密钥不存在它会崩溃)但是如果我检查

[whatever objectForKey:@"status"] == nil
Run Code Online (Sandbox Code Playgroud)

当它是零时它没关系,但是如果它不存在它会崩溃(因为我收回了我的其他回报)

我得到的崩溃就是这个,似乎NSJSONSerialzation将NSArray作为NSdictionary返回?:

2012-07-02 11:03:40.426 [9412:10703] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x75f03f0
2012-07-02 11:03:40.429 [9412:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x75f03f0'
Run Code Online (Sandbox Code Playgroud)

我需要找出一种方法来了解我得到了哪些回报,以便我可以采取相应行动.

Oma*_*ith 5

你可以检查whatever变量的类型

if([whatever isKindOfClass:[NSDictionary class]])
    //then check for nil
else
    //Then its not a dictionary then in your case its empty
Run Code Online (Sandbox Code Playgroud)