如何在ios中检查json响应中是否可用的密钥

时间:2013-05-18 12:48:18

标签: iphone ios json

在我向服务器请求后,我收到了两种不同类型的响应

如果是成功回复就是这个

[{"id":5,"is_default":0,"name":"Dutch","language_id":3},{"id":4,"is_default":1,"name":"French","language_id":2}]

其他响应类型将是

 {"status":102,"response":"Empty record list."}

是否有任何方法可以在objc中检测“状态”键是否可用。 感谢

找到了解决方案

   //break it from result tag    
if([[responseString JSONValue] isKindOfClass:[NSDictionary class]]){

    NSDictionary *dict = [responseString JSONFragmentValue];

    if([dict count]==2){
      return;
    }
    //nothing to load

}

2 个答案:

答案 0 :(得分:2)

Parse JSONobject的回复。检查解析object是否为dictionary,然后检查密钥"status"

如果解析后的objectarray,请从results访问index

答案 1 :(得分:2)

响应只是一个字典,所以要查看你是否有你想要的密钥,你可以使用NSDictionary方法:

// Assume you have already created a dictionary, jsonResponse, from your JSON

// First, get all the keys in the response

NSArray *responseKeys = [jsonResponse allKeys];

// Now see if the array contains the key you are looking for

BOOL isErrorResponse = [responseKeys containsObject:@"status"];