ios - 检查 JSON 对象是否为 null swift 3

标签 ios swift swift3 native

我很难检查此 JSON 对象是否为空。包含和不包含客户 key 的示例 JSON。

这就是 finalValue 等于

使用客户 key :

{
  "customer" : { 
      "href" : "myURL"
  },
  "token": "781hjasf98123bjwef8"
}

没有客户 key :

{ 
    "error" : {
        "message" : "Unauthorized"
    }
}

这就是我尝试检查的方式,但它总是转到 else 语句。

            if let value = response.result.value{
                let finalValue = JSON(value)
                if finalValue["customer"] is NSNull{
                    print("if part")
                }else{
                    print("Else part")
                }
            }

最佳答案

您可以只使用可选链接:

if let finalValue = finalValue["customer"] as? String {
    // there's a value!
}

关于ios - 检查 JSON 对象是否为 null swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45958493/

相关文章:

ios - 如果用户不允许快速通知,则不会调用 didRefreshRegistrationToken

swift - 二元运算符 '==' 不能应用于类型 'Int8' 和 'Int' 的操作数

ios - 使用 Swift 3 和 Alamofire 4 后没有得到响应

ios - 在ios/xcode中可以打开自动布局但没有限制吗

ios - 使用自定义单元格在 UITableView 中展开 UILabel 时 Swift 崩溃

ios - PDF 文件无法在 UIDocumentInteractionController 中打开

ios - 具有顺序和限制的 Firestore 复合查询

arrays - Swift:二进制搜索标准数组?

ios - uipageviewcontroller 禁用向右滑动

iOS - 单击选项卡图标时重新加载 UIView?