iphone - 如何检查 ios 中的 json 响应中的 key 是否可用

标签 iphone ios json

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

如果成功响应就是这个

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

}

最佳答案

响应只是一个字典,所以要查看您是否有您正在寻找的键,您可以使用 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"];

关于iphone - 如何检查 ios 中的 json 响应中的 key 是否可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624431/

相关文章:

iPhone 4 根据 GPS 纬度和一年中的某一天以编程方式计算日持续时间

android - 移动开发推荐

javascript - 如何仅获取一次变量中的对象总数?

java - 无法让 getOutputStream() 在 servlet 中工作

iPhone 颜色选择器

ios - 重新加载 searchResultsTableView 时自定义单元格错误

iPhone Google Analytics - 识别唯一用户

ios - 我如何在标签栏项目上添加自定义 View

java - 在 Tomcat 服务器(本地)中的 servelt 运行期间,无法加载 Intellij Json 对象

iphone - 如何将 CCTransitionPageTurn 方向设置为垂直?