如何解决如何从响应数据结构构造 Codable
如何从响应数据中对 Any & JSONNull 进行编码
Json 响应数据
"userType": {
"id": "5f18J20a21n","name": "userName","name_prefix": null,"group": []
}
用户类型模型类
struct UserType : Codable {
let id : String?
let name : String?
let namePrefix: JSONNull? // I replace JSONNull with String,Is it good way to do so?
let group : [JSONAny?]?
enum CodingKeys: String,CodingKey {
case id = "id"
case name = "name"
case namePrefix = "name_prefix"
case group = "group"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decodeIfPresent(String.self,forKey: .id)
name = try values.decodeIfPresent(String.self,forKey: .name)
namePrefix = try values.decodeIfPresent(String.self,forKey: . namePrefix)
// namePrefix = try values.decodeIfPresent(JSONNull.self,forKey: . namePrefix)
group = try values.decodeIfPresent([JSONAny].self,forKey: .group)
// Type of expression is ambiguous without more context
}
}
表达类型不明确,没有更多上下文
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。