复杂 Json 到 Swift 可编码
Complex Json to Swift Codable
我在尝试将我的 JSON 响应“投射”到我的结构时遇到问题。
我的 JSON 回复:
{
"data" = {
"answers" = {
"10" = "Not";
"11" = "Not";
};
"company" = 1;
"name" = "Name";
"profile" = {
"email" = "email@email.com";
"first_name" = "First name";
"identity_document" = 12345678;
};
};
"msg_code" = 0;
"msg_text" = "Success";
}
我的结构:
struct LoginResponse: Codable {
let answers: Dictionary<String, String>?
let company: Int?
let name: String?
let profile: Profile?
private enum CodingKeys: String, CodingKey{
case answers = "answers"
case company = "company"
case name = "name"
case profile = "profile"
}
}
struct Profile: Codable{
let email: String?
let first_name: String?
let identity_document: String?
private enum CodingKeys: String, CodingKey{
case email = "email"
case first_name = "first_name"
case identity_document = "identity_document"
}
}
我要解码的代码:
Alamofire.request("myURL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON{
response in
switch response.result
{
case .success(let json):
let login = try! JSONDecoder().decode(LoginResponse.self, from: json as! Data)
SVProgressHUD.dismiss()
case .failure(let error):
self.showAlertOk(title:"Alert!", message: "Response Error", handlerOK: { action in print("error")})
SVProgressHUD.dismiss()
}
}
行:
let login = try! JSONDecoder().decode(LoginResponse.self, from: json as! Data)
修复上个版本的结果:
let login = try! JSONDecoder().decode(LoginResponse.self, from: json)
或
let login = try! JSONDecoder().decode(LoginResponse.self, from: json.data(using: .utf8)!)
logcat表示,
Could not cast value of type '__NSDictionaryI' (0x7fff87b9d5f0) to 'NSData' (0x7fff87b9c088).
有什么建议吗?我明白我必须改变!作为字典的数据,但我没有找到任何示例如何做。
两个致命问题:
根对象(键为 data
、msg_code
和 msg_text
的字典)丢失
struct Root: Codable {
let data : LoginResponse
}
你必须用responseData
替换responseJSON
才能得到原始数据,responseJSON
returns一个Swift数组或字典.
Alamofire.request("myURL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseData {
...
case .success(let data):
let login = try! JSONDecoder().decode(Root.self, from: data)
并且不要 try!
、catch
错误并处理它。
我在尝试将我的 JSON 响应“投射”到我的结构时遇到问题。
我的 JSON 回复:
{
"data" = {
"answers" = {
"10" = "Not";
"11" = "Not";
};
"company" = 1;
"name" = "Name";
"profile" = {
"email" = "email@email.com";
"first_name" = "First name";
"identity_document" = 12345678;
};
};
"msg_code" = 0;
"msg_text" = "Success";
}
我的结构:
struct LoginResponse: Codable {
let answers: Dictionary<String, String>?
let company: Int?
let name: String?
let profile: Profile?
private enum CodingKeys: String, CodingKey{
case answers = "answers"
case company = "company"
case name = "name"
case profile = "profile"
}
}
struct Profile: Codable{
let email: String?
let first_name: String?
let identity_document: String?
private enum CodingKeys: String, CodingKey{
case email = "email"
case first_name = "first_name"
case identity_document = "identity_document"
}
}
我要解码的代码:
Alamofire.request("myURL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON{
response in
switch response.result
{
case .success(let json):
let login = try! JSONDecoder().decode(LoginResponse.self, from: json as! Data)
SVProgressHUD.dismiss()
case .failure(let error):
self.showAlertOk(title:"Alert!", message: "Response Error", handlerOK: { action in print("error")})
SVProgressHUD.dismiss()
}
}
行:
let login = try! JSONDecoder().decode(LoginResponse.self, from: json as! Data)
修复上个版本的结果:
let login = try! JSONDecoder().decode(LoginResponse.self, from: json)
或
let login = try! JSONDecoder().decode(LoginResponse.self, from: json.data(using: .utf8)!)
logcat表示,
Could not cast value of type '__NSDictionaryI' (0x7fff87b9d5f0) to 'NSData' (0x7fff87b9c088).
有什么建议吗?我明白我必须改变!作为字典的数据,但我没有找到任何示例如何做。
两个致命问题:
根对象(键为
data
、msg_code
和msg_text
的字典)丢失struct Root: Codable { let data : LoginResponse }
你必须用
responseData
替换responseJSON
才能得到原始数据,responseJSON
returns一个Swift数组或字典.Alamofire.request("myURL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseData { ... case .success(let data): let login = try! JSONDecoder().decode(Root.self, from: data)
并且不要 try!
、catch
错误并处理它。