需要帮助解码包含数组的 Json
Need help decoding Json that contains an array
我需要做以下事情:
定义两个 Swift classes 来解码 JSON 字符串
解码JSON字符串得到两个classes
的对象
这是我要解码的 JSON :
{“status":200,"holidays":[{"name":"Thanksgiving","date":"2017-10-09","observed":"2017-10-09","public":false}]}
我已经尝试创建两个 classes 并且在主 class
中调用 class 时我得到的结果是什么
class HolidayItems : Decodable {
let name : String?
let date : String?
let observed: String?
let `public` : Bool?
private enum CodingKeys: String, CodingKey {
case name
case date
case observed
case `public`
}
required init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
date = try container.decode(String.self, forKey: .date)
observed = try container.decode(String.self, forKey: .observed)
`public` = try container.decode(Bool.self, forKey: .`public`)
}
} // HolidayItems
class HolidayAPI: Decodable {
let status: HolidayItems
// let holiday :[HolidayItems]
func getHolidayName() -> String {
return status.name ?? "no advice, server problem"
}
func getAdviceNo() -> String {
return status.date ?? ""
}
private enum CodingKeys: String, CodingKey {
case status
case holiday = "items"
}
required init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
status = try container.decode(HolidayItems.self, forKey: .status)
// holiday = try container.decode(HolidayItems.self, forKey: .holiday)
}
}
这是我应该得到的结果:
Optional("Thanksgiving")
Optional("2017-10-09")
我在 return
中一无所获
您的响应是在根级别上,只是 status
类型 Int
的对象和另一个对象的一个数组
注意
- 您不必实施自定义
CodingKey
- 您不需要自定义
init
和 Decoder
- 您的模型可以
struct
- 您可以将
HolidayItems
重命名为 Holiday
struct HolidayAPI: Decodable {
let status: Int
let holidays: [Holiday]
}
struct Holiday: Decodable {
let name, date, observed: String
let `public`: Bool
}
那么当你需要获取某个节日道具时,获取holidays
的某个元素即可
decodedResponse.holidays[0].name
我需要做以下事情: 定义两个 Swift classes 来解码 JSON 字符串
解码JSON字符串得到两个classes
的对象这是我要解码的 JSON :
{“status":200,"holidays":[{"name":"Thanksgiving","date":"2017-10-09","observed":"2017-10-09","public":false}]}
我已经尝试创建两个 classes 并且在主 class
中调用 class 时我得到的结果是什么class HolidayItems : Decodable {
let name : String?
let date : String?
let observed: String?
let `public` : Bool?
private enum CodingKeys: String, CodingKey {
case name
case date
case observed
case `public`
}
required init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
date = try container.decode(String.self, forKey: .date)
observed = try container.decode(String.self, forKey: .observed)
`public` = try container.decode(Bool.self, forKey: .`public`)
}
} // HolidayItems
class HolidayAPI: Decodable {
let status: HolidayItems
// let holiday :[HolidayItems]
func getHolidayName() -> String {
return status.name ?? "no advice, server problem"
}
func getAdviceNo() -> String {
return status.date ?? ""
}
private enum CodingKeys: String, CodingKey {
case status
case holiday = "items"
}
required init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
status = try container.decode(HolidayItems.self, forKey: .status)
// holiday = try container.decode(HolidayItems.self, forKey: .holiday)
}
}
这是我应该得到的结果:
Optional("Thanksgiving") Optional("2017-10-09")
我在 return
中一无所获您的响应是在根级别上,只是 status
类型 Int
的对象和另一个对象的一个数组
注意
- 您不必实施自定义
CodingKey
- 您不需要自定义
init
和Decoder
- 您的模型可以
struct
- 您可以将
HolidayItems
重命名为Holiday
struct HolidayAPI: Decodable {
let status: Int
let holidays: [Holiday]
}
struct Holiday: Decodable {
let name, date, observed: String
let `public`: Bool
}
那么当你需要获取某个节日道具时,获取holidays
decodedResponse.holidays[0].name