0

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

如果是成功响应将是这个

[{"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

}
4

2 回答 2

2

ParseJSON响应objectobject检查是否已解析dictionary,然后检查密钥"status"

如果 parsedobject是一个array,则访问resultsfrom index

于 2013-05-18T12:51:00.680 回答
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"];
于 2013-05-18T12:54:07.540 回答