检查JSON对象null swift 3

问题描述:

我很难检查这个JSON对象是否为null。使用和不使用客户密钥的示例JSON。检查JSON对象null swift 3

这是最终值等于=

随着用户密码:

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

没有用户密码:

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

这是我尝试检查,但它总是去别的声明。

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

是你解决问题吗? – Hitesh

雨燕3.0

使用SwiftyJSON,有检查的关键是存在或无法正常工作exists()

if let value = response.result.value { 
     let finalValue = JSON(value) 
     if finalValue["customer"].exists() { 
      print("if value exists then this part will be executed") 
     } else { 
      print("if value no longer then this part will be executed") 
     } 
} 

你可以使用可选链接

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