Compare keys from json service response
-
28-05-2021 - |
Question
I am getting a json service to which I want to do comparison checks on the values of certain keys. For example I have a key 'status' in json array, but I am unable to do a comparison check on its value. The response is like:
{"status":400,"response":"No cast found"}
I want to do something like:
if([jsonDict objectForKey:@"status"] == @"400"){
..
}
but the check wont work, whats the right way to do it?
Solution
It can be two things:
If [jsonDict objectForKey:@"status"] is giving you a NSString, then the correct way to compare is:
if([[jsonDict objectForKey:@"status"] isEqualToString:@"400"])
If [jsonDict objectForKey:@"status"] is giving you a NSNumber, or NSDecimalNumber:
if([[jsonDict objectForKey:@"status"] isEqualToNumber:[NSNumber numberWithInt:400])
OTHER TIPS
Try this
if([jsonDict objectForKey:@"status"] isEqualToString @"400"){
..
}
You can also do it in this way:
if([[jsonDict objectForKey:@"status"] intValue] == 400){
//your code here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow