使用postman验证json响应(这些Room对象的列表)
{
"id": "506e9a1d-cded-4853-a9df-7f8a57d6550d",
"name": "TeleTwo",
"shortName": "2",
"displayName": "2",
"inService": true,
"managed": true,
"capacity": 2,
"schedulingOption": "VC Scheduling",
"location": {
"id": "50b07258-2668-416e-aef4-a8a48e9e7389",
"name": "Telepresence Test",
"shortName": "Telepresence Test",
"displayName": "Telepresence Test",
"city": "Nash",
"country": "United States",
"account": {
"id": "509788c6-197f-40f1-a7f7-274a23af9062",
"name": "Development",
"shortName": "Dev",
"displayName": "Dev",
"active": true,
"exchangeEnabled": false,
"tmsEnabled": false,
"commProxyEnabled": true,
"services": [
"Premier",
"VMR_Pool_Static",
"VMR"
],
"logo": "/provisioning/image/2472"
},
"latitude": 45.786368,
"longitude": -78.505822,
"active": false,
"summary": null
},
"summary": {
"totalDevices": 2
},
"vip": false
}我在站点json4swift.com上使用了这个json (完整列表,上面的缩写)来构建我的dto对象,并将它们放在我的项目中。
再次缩短长度,如果需要的话,我可以添加更多(更新后包含所有内容,因为这是问题的一部分):
Room.swift
struct Room : Codable {
let id : String?
let name : String?
let shortName : String?
let displayName : String?
let inService : Bool?
let managed : Bool?
let capacity : Int?
let schedulingOption : String?
let location : Location?
let summary : RoomSummary?
let vip : Bool?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case shortName = "shortName"
case displayName = "displayName"
case inService = "inService"
case managed = "managed"
case capacity = "capacity"
case schedulingOption = "schedulingOption"
case location
case summary
case vip = "vip"
}
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)
shortName = try values.decodeIfPresent(String.self, forKey: .shortName)
displayName = try values.decodeIfPresent(String.self, forKey: .displayName)
inService = try values.decodeIfPresent(Bool.self, forKey: .inService)
managed = try values.decodeIfPresent(Bool.self, forKey: .managed)
capacity = try values.decodeIfPresent(Int.self, forKey: .capacity)
schedulingOption = try values.decodeIfPresent(String.self, forKey: .schedulingOption)
location = try Location(from: decoder)
summary = try RoomSummary(from: decoder)
vip = try values.decodeIfPresent(Bool.self, forKey: .vip)
}}
Location.swift
struct Location : Codable {
let id : String?
let name : String?
let shortName : String?
let displayName : String?
let city : String?
let country : String?
let account : Account?
let latitude : Double?
let longitude : Double?
let active : Bool?
let summary : LocationSummary?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case shortName = "shortName"
case displayName = "displayName"
case city = "city"
case country = "country"
case account
case latitude = "latitude"
case longitude = "longitude"
case active = "active"
case summary
}
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)
shortName = try values.decodeIfPresent(String.self, forKey: .shortName)
displayName = try values.decodeIfPresent(String.self, forKey: .displayName)
city = try values.decodeIfPresent(String.self, forKey: .city)
country = try values.decodeIfPresent(String.self, forKey: .country)
account = try Account(from: decoder)
latitude = try values.decodeIfPresent(Double.self, forKey: .latitude)
longitude = try values.decodeIfPresent(Double.self, forKey: .longitude)
active = try values.decodeIfPresent(Bool.self, forKey: .active)
summary = try LocationSummary(from: decoder)
}
}Account.swift
struct Account : Codable {
let id : String?
let name : String!
let shortName : String!
let displayName : String!
let active : Bool!
let exchangeEnabled : Bool!
let tmsEnabled : Bool!
let commProxyEnabled : Bool!
let services : [String]!
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case shortName = "shortName"
case displayName = "displayName"
case active = "active"
case exchangeEnabled = "exchangeEnabled"
case tmsEnabled = "tmsEnabled"
case commProxyEnabled = "commProxyEnabled"
case services = "services"
}
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)
shortName = try values.decodeIfPresent(String.self, forKey: .shortName)
displayName = try values.decodeIfPresent(String.self, forKey: .displayName)
active = try values.decodeIfPresent(Bool.self, forKey: .active)
exchangeEnabled = try values.decodeIfPresent(Bool.self, forKey: .exchangeEnabled)
tmsEnabled = try values.decodeIfPresent(Bool.self, forKey: .tmsEnabled)
commProxyEnabled = try values.decodeIfPresent(Bool.self, forKey: .commProxyEnabled)
services = try values.decodeIfPresent([String].self, forKey: .services)
}
}我的问题是,Room.Location & Room.Location.Account是Room的副本,参见下面的截图。

为什么它会这样反序列化我的对象呢?
发布于 2018-05-18 23:44:53
我检查了您的代码,并作了一些修改,否则代码无法工作:我使用了相同的json,但更正了summary属性类型:
struct Room : Codable {
let id : String?
let name : String?
let shortName : String?
let displayName : String?
let inService : Bool?
let managed : Bool?
let capacity : Int?
let schedulingOption : String?
let location : Location?
let summary : Dictionary<String, Int>?
let vip : Bool?
}
struct Location : Codable {
let id : String?
let name : String?
let shortName : String?
let displayName : String?
let city : String?
let country : String?
let account : Account?
let latitude : Double?
let longitude : Double?
let active : Bool?
let summary : String?
}
struct Account : Codable {
let id : String?
let name : String!
let shortName : String!
let displayName : String!
let active : Bool!
let exchangeEnabled : Bool!
let tmsEnabled : Bool!
let commProxyEnabled : Bool!
let services : [String]!
}
do {
let jsonDecoder = JSONDecoder()
let object = try jsonDecoder.decode(Room.self, from: data) // Here is Room.self, not [Room].self, due to json root object is dictionary
print("object: ", object)
} catch {
print("error: ", error)
}在作品和印刷方面:
对象:房间(id: Optional("506e9a1d-cded-4853-a9df-7f8a57d6550d"),名称:可选(“TeleTwo”),shortName:可选(“2”),displayName:可选(“2”),inService:可选(真),托管:可选(真),容量:可选(2),schedulingOption:可选(“VC调度”),位置(id: Optional("50b07258-2668-416e-aef4-a8a48e9e7389"),名称:可选(“远程检查”),shortName:可选(“远程检查”),可选(“远程测试”),城市:可选(“纳什”),国家:可选(“美国”),帐户:可选(id: Optional("509788c6-197f-40f1-a7f7-274a23af9062"),名称: Swift.ImplicitlyUnwrappedOptional.some("Development"),shortName: Swift.ImplicitlyUnwrappedOptional.some("Dev"),displayName: Swift.ImplicitlyUnwrappedOptional.some("Dev"),active: Swift.ImplicitlyUnwrappedOptional.some(真),exchangeEnabled: Swift.ImplicitlyUnwrappedOptional.some(false),( Swift.ImplicitlyUnwrappedOptional.some(false),commProxyEnabled: Swift.ImplicitlyUnwrappedOptional.some(真),服务: Swift.ImplicitlyUnwrappedOptional>.some("Premier","VMR_Pool_Static",“VMR”),纬度:可选(45.786368000000003),经度:可选(-78.505821999999995),活动:可选(假),摘要:零),摘要:可选(totalDevices: 2),要员:可选(假)
看来一切都还好。
https://stackoverflow.com/questions/50420124
复制相似问题