【发布时间】:2019-02-06 04:07:20
【问题描述】:
我正在尝试将 Json 数据解码到我的模型中。 这是我的模型
struct Devices : Codable {
var id :String?
var description :String?
var status : Int?
}
var heroes = Devices()
print(DeviceId)
let loginParam: [String: Any] = [
"id": DeviceId
]
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 5
manager.request("http://13.13.13.004/website/api/Customer/DeviceControl", method: .post , parameters: loginParam, encoding: JSONEncoding.prettyPrinted)
.responseData { response in
let json = response.data
do{
let decoder = JSONDecoder()
//using the array to put values
heroes = try decoder.decode(Devices.self, from: json!)
}catch let err{
print(err)
}
这段代码没有进入 catch 块。 但是英雄值返回零。 当我尝试使用 NsDictionary 给出结果。
【问题讨论】:
-
你能不能:
let jsonString = String(data: json, encoding: .utf8); print("JSON: \(jsonString)并告诉我们输出是什么? -
@Larme 给出正确的结果
JSON: Optional("{\"resultCount\":1,\"results\":[{\"id\":\"haktaneb\",\"status\":0,\"description\":\"asfasf\"}]}")
标签: swift alamofire jsondecoder