我正在尝试验证我的响应状态代码是否等于200,但==或!=不起作用。请给一些样品。
JSON Response:{
message = "registered Successfull";
result = {
"created_by" = "2012-03-14 07:26:31";
"created_ts" = "2013-03-14 07:26:31";
"c_email" = "jk@mail.com";
"c_lastname" = hi;
"c_name" = sc;
id = 123;
login = {
authorization = z2VIownsD0Pr1jIMhJE;
};
"phone_no" = 123XXXX;
};
status = 1;
"status_code" = 200;
}
下面的我的代码
let status_code = result["status_code"] as? [String: AnyObject]
if (status_code! == "200") {
print("ERROR:\(String(describing: status_code))")
} else {
let results = result["result"] as? [String: AnyObject]
if let login = results!["login"] as? [String: AnyObject] {
let authorization = login["authorization"] as? String
print(authorization ?? "")
}
self.navigatepop() // Not working
}
答案 0 :(得分:1)
如果 status_code 位于String
if let json = response.result.value as? [String: Any] {
let status_code : String = json["status_code"]! as! String
// here you can check if it is in Integer format
if(status_code == "200") {
//Your code after success code
}
}
答案 1 :(得分:0)
if let json = response.result.value{
let responseDict = json as! NSDictionary
if let statusCode = responseDict.object(forKey: "status_code") as? Int {
print(statusCode)
}
}
答案 2 :(得分:0)
if let jsonDictionary = response.result.value as? [String: Any], let status_code : Int = jsonDictionary["status_code"] as? Int {
if(status_code == 200) {
//Your success code
if let results = jsonDictionary["result"] as? [String: AnyObject], let login = results["login"] as? [String: AnyObject] , let authorization = login["authorization"] as? String {
print(authorization )
} else {
self.navigatepop() // status_code == 200 but
}
} else {
// your else code , status_code != 200
}
}