Mr.G
Mr.G

Reputation: 1275

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. 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

}

Upvotes: 0

Views: 1829

Answers (2)

Apurv
Apurv

Reputation: 17186

Parse the JSON response into object. Check if parsed object is dictionary, then check the key "status".

If the parsed object is an array, then access the results from index.

Upvotes: 2

Abizern
Abizern

Reputation: 150615

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:

// 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"];

Upvotes: 2

Related Questions