gpt4 book ai didi

ios - if 条件在 swift ios 的 json 响应中

转载 作者:行者123 更新时间:2023-11-30 12:41:17 26 4
gpt4 key购买 nike

如果服务器响应是(结果=“帐户存在”),那么我可以做一些事情。但是当我比较结果时,它给出一个错误:“二元运算符‘==’不能应用于‘Any’和‘String’类型的操作数”。

let dataTask = session.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in

do
{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary

print("JSON : \(json)")

if let dataDic = json.value(forKey: "data")
{
print("dataDic: \(dataDic)")

if let Result = (dataDic as AnyObject).value(forKey: "Result")
{
print("Result: \(Result)")

if (Result ("Account Exists")) == 0
{
//... DO Something
}
}
}
}
catch
{
print("Catch exception")
}
})
dataTask.resume()
}

最佳答案

以纯粹快捷的方式..您的“数据”是一个数组,顶级 json 是字典

   do
{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]

if let data = json["data"] as? [[String:String]] {
if let resultString = data.first?["Result"] as? String{
if resultString == "Account Exists"{
//... DO Something
}
else if resultString == "Unauthorized Access" {
//... DO Something
}
}
}
}
catch{
print("Catch exception")
}

关于ios - if 条件在 swift ios 的 json 响应中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42201721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com