[英]how to check key available or not in json response in ios
请求服务器后,我会收到两种不同类型的响应
如果是成功的回应将是这个
[{"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
}
将JSON
响应Parse
为object
。 检查解析的object
是否为dictionary
,然后检查键"status"
。
如果解析的object
是array
,则从index
访问results
。
响应只是一个字典,因此要查看是否拥有要查找的密钥,可以使用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.