您好我尝试从请求打印但是没有
Alamofire.request("http://.../api/v1.8/set/order/", method: .get, parameters: params)
.responseString { response in
switch response.result {
case .success(let value):
print("value\(value)")
var json = JSON(value)
let nass = json["description"].string
print("nass\(nass)")
}
在cinsole打印我
value {"result":{"code":"15","description":"Invalid account email"}}
nass nil
答案 0 :(得分:1)
Alamofire.request("http://xincal.ru/api/v1.8/set/order/", method: .get, parameters: params).responseJSON { response in
if let r = response.result.value as? NSDictionary
if let result = r?.value(forKey: "result") as? NSDictionary {
if let nass = result.value(forKey: "description") as? String {
print(nass)
}
}
} else {
print("Connection error")
}
}
但我们有更好的方法使用Codable解析响应数据。
为响应准备模型
struct Result: Codable {
let code: String
let description: String
}
struct Response: Codable {
let result: Result
}
Alamofire.request("http://xincal.ru/api/v1.8/set/order/", method: .get, parameters: params).responseJSON { response in
if let data = response.data
let object = try! JSONDecoder().decode(Response.self, from: jsonData)
print(object.result.description)
} else {
print("Connection error")
}
}