我是新来的斯威夫特,我开始用PHP服务器斯威夫特4项目。我用Alamofire的请求,并使用print()
打印数据。这就是我得到:
{"error":false,"n":"Raghad"}
但是,当我想将其转换为字符串,返回""
(空),当我转换为Boolean它正确返回值。
那么,如何解决这个问题?
let wJSON : JSON = JSON(response.result.value!)
print(wJSON["n"].stringValue)
print(wJSON["error"].boolValue)
使用Decodable
简单的解决方案,定义符合Decodable
协议你的字典一个struct
struct Reply: Decodable {
let error: Bool
let n: String
}
let data = response.data
do {
let result = try JSONDecoder().decode(Reply.self, from: data)
print("\(result.n) \(result.error)")
} catch {
print(error)
}
我改变responseString
到responseJSON
Alamofire.request(URL!方法:.POST,参数:PAR).validate(){responseJSON
响应中,如果response.result.isSuccess {让wJSON:(!response.result.value)JSON = JSON
和它的工作