Swift 4 JSONDecoder 复杂数据丢失键
Swift 4 JSONDecoder Complex Data Missing Key
我已经为这个问题苦苦挣扎了一天多了,我不确定为什么当我在我的结构中明确定义密钥时,我总是收到丢失密钥错误。非常感谢您的帮助。请帮助消除我对如何解码 JSON.
中的嵌套类型的困惑
我正在尝试解析以下 JSON 响应,但当我 运行 我的应用程序时,我不断收到“缺少密钥”错误消息。以下是我从该服务返回的消息。
这是 JSON 回复
{
UserInfo = {
ApplicationArea = {
CreationDateTime = "2018-02-11 21:34:40.646000";
MfgCode = PSUN;
Sender = DM;
};
ServiceStatus = {
StatusCode = 0;
StatusDescription = Success;
System = "Product Info";
};
UserInfoDataArea = {
Email = "john@example.com";
Locale = "en_US";
UserId = 3;
UserName = jdoe;
};
};
}
这是错误
Missing Key: applicationArea
Debug description: No value associated with key applicationArea ("ApplicationArea").
下面是我获取请求和解码响应的结构代码。
struct UserInfo:Codable {
struct ApplicationArea:Codable {
let creationDateTime: String
let mfgCode: String
let sender: String
private enum CodingKeys: String, CodingKey {
case creationDateTime = "CreationDateTime"
case mfgCode = "MfgCode"
case sender = "Sender"
}
}
let applicationArea: ApplicationArea
enum CodingKeys: String, CodingKey {
case applicationArea = "ApplicationArea"
}
}
创建请求的代码
let apiMethod = HTTPMethod.post
Alamofire.request(
loginURL!,
method: apiMethod,
parameters: params,
encoding: URLEncoding.default,
headers: header)
.responseJSON { (response) -> Void in
switch response.result {
case .success:
print(response.result.value)
let result = response.data
do {
let decoder = JSONDecoder()
let userDetails = try decoder.decode(UserInfo.self, from: result!)
print("Response \(userDetails)")
} catch DecodingError.keyNotFound(let key, let context) {
print("Missing Key: \(key)")
print("Debug description: \(context.debugDescription)")
} catch {
print("Error \(error.localizedDescription)")
}
case .failure(let error):
print("Error \(error)")
}
}
您犯了一个很常见的错误:您忽略了根对象,即包含 UserInfo
键的最外层字典。
创建一个Root
结构
struct Root: Decodable {
let userInfo : UserInfo
enum CodingKeys: String, CodingKey { case userInfo = "UserInfo" }
}
并解码
let root = try decoder.decode(Root.self, from: result!)
let userDetails = root.userInfo
我已经为这个问题苦苦挣扎了一天多了,我不确定为什么当我在我的结构中明确定义密钥时,我总是收到丢失密钥错误。非常感谢您的帮助。请帮助消除我对如何解码 JSON.
中的嵌套类型的困惑我正在尝试解析以下 JSON 响应,但当我 运行 我的应用程序时,我不断收到“缺少密钥”错误消息。以下是我从该服务返回的消息。
这是 JSON 回复
{
UserInfo = {
ApplicationArea = {
CreationDateTime = "2018-02-11 21:34:40.646000";
MfgCode = PSUN;
Sender = DM;
};
ServiceStatus = {
StatusCode = 0;
StatusDescription = Success;
System = "Product Info";
};
UserInfoDataArea = {
Email = "john@example.com";
Locale = "en_US";
UserId = 3;
UserName = jdoe;
};
};
}
这是错误
Missing Key: applicationArea
Debug description: No value associated with key applicationArea ("ApplicationArea").
下面是我获取请求和解码响应的结构代码。
struct UserInfo:Codable {
struct ApplicationArea:Codable {
let creationDateTime: String
let mfgCode: String
let sender: String
private enum CodingKeys: String, CodingKey {
case creationDateTime = "CreationDateTime"
case mfgCode = "MfgCode"
case sender = "Sender"
}
}
let applicationArea: ApplicationArea
enum CodingKeys: String, CodingKey {
case applicationArea = "ApplicationArea"
}
}
创建请求的代码
let apiMethod = HTTPMethod.post
Alamofire.request(
loginURL!,
method: apiMethod,
parameters: params,
encoding: URLEncoding.default,
headers: header)
.responseJSON { (response) -> Void in
switch response.result {
case .success:
print(response.result.value)
let result = response.data
do {
let decoder = JSONDecoder()
let userDetails = try decoder.decode(UserInfo.self, from: result!)
print("Response \(userDetails)")
} catch DecodingError.keyNotFound(let key, let context) {
print("Missing Key: \(key)")
print("Debug description: \(context.debugDescription)")
} catch {
print("Error \(error.localizedDescription)")
}
case .failure(let error):
print("Error \(error)")
}
}
您犯了一个很常见的错误:您忽略了根对象,即包含 UserInfo
键的最外层字典。
创建一个Root
结构
struct Root: Decodable {
let userInfo : UserInfo
enum CodingKeys: String, CodingKey { case userInfo = "UserInfo" }
}
并解码
let root = try decoder.decode(Root.self, from: result!)
let userDetails = root.userInfo