Ios 当json包含没有键的数组时,如何检查swiftyJSON中是否存在键

Ios 当json包含没有键的数组时,如何检查swiftyJSON中是否存在键,ios,json,swift,swifty-json,Ios,Json,Swift,Swifty Json,我知道swiftyJSON方法exists(),但它似乎并不总是像他们说的那样工作。 在下面这种情况下,我如何才能得到正确的结果?我不能改变JSON结构,因为我是通过客户端的API来实现的 var json: JSON = ["response": ["value1","value2"]] if json["response"]["someKey"].exists(){ print("response someKey exists") } 输出: response someKey e

我知道swiftyJSON方法exists(),但它似乎并不总是像他们说的那样工作。 在下面这种情况下,我如何才能得到正确的结果?我不能改变JSON结构,因为我是通过客户端的API来实现的

var json: JSON =  ["response": ["value1","value2"]]
if json["response"]["someKey"].exists(){
    print("response someKey exists")
}
输出:

response someKey exists 响应someKey存在
不应该打印它,因为someKey不存在。但有时该密钥来自客户端的API,我需要找出它是否存在或不正确。

在您的情况下,它不起作用,因为
json[“response”]
的内容不是字典,而是数组。SwiftyJSON无法检查数组中的有效字典键

使用字典时,它会正常工作,但不会按预期执行条件:

var json: JSON =  ["response": ["key1":"value1", "key2":"value2"]]
if json["response"]["someKey"].exists() {
    print("response someKey exists")
}
解决此问题的方法是在使用
.exists()
之前检查内容是否确实是词典:

if let _ = json["response"].dictionary {
    if json["response"]["someKey"].exists() {
        print("response someKey exists")
    }
}