ios - 如何从 Swift3/Swift4 中的 JSON 中获取 bool 的值?

标签 ios json swift swift4

let responseString = String(data: data, encoding: .utf8)

if responseString["is_valid"] == true {
    print("Login Successful")
} else {
    print("Login attempt failed")
}

我正在尝试从 responseString 字典中获取 "is_valid" 的值。但是,我不知道如何。我尝试过的一切都失败了。

responseString 当通过 print() 输出时看起来像这样:

{
  "is_valid": true
}

最佳答案

你可以这样使用:

if let responseString = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Bool] {
    if responseString!["is_valid"] == true {
        print("Login Successful")
    } else {
        print("Login attempt failed")
    }
}

关于ios - 如何从 Swift3/Swift4 中的 JSON 中获取 bool 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46625552/

相关文章:

ios - 饼图标签与小型设备中的饼图重叠

ios - 着色/绘图代码。如何以较低的不透明度使其更光滑?去除刷点?

ios - UIImage(contentsOfFile) 的内存问题

json - 使用 Jackson Json 解析器递归解析 json 文件

php - 将android应用程序连接到mysql数据库

ios - 使用 AudioToolbox.framework 循环播放音频

java - 尝试将 RowItem 添加到 ListView 时 Android IndexOutOfBoundsException

swift - 在 RealityKit 场景上应用着色器

ios - 游戏中心 "Done"按钮问题?

ios - 具有许多 UIView 的单例服务