简体   繁体   English

如何在iOS中的JSON响应中检查密钥是否可用

[英]how to check key available or not in json response in ios

After i request to the server , i recieve two different type of response 请求服务器后,我会收到两种不同类型的响应

if it is success response will be this 如果是成功的回应将是这个

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

other response type would be 其他响应类型是

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

Is the any way to detect whether the "status" key is available or not in response in objc. 检测objc中的“状态”键是否可用的任何方法。 Thanks 谢谢

found a solution 找到了解决方案

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

    NSDictionary *dict = [responseString JSONFragmentValue];

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

}

Parse the JSON response into object . JSON响应Parseobject Check if parsed object is dictionary , then check the key "status" . 检查解析的object是否为dictionary ,然后检查键"status"

If the parsed object is an array , then access the results from index . 如果解析的objectarray ,则从index访问results

The response is just a dictionary, so to see if you have the key you are looking for you can just use the NSDictionary methods: 响应只是一个字典,因此要查看是否拥有要查找的密钥,可以使用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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM