【发布时间】:2018-11-15 17:46:02
【问题描述】:
我有 JSON 结构 为:
"periods": {
"2018-06-07": [
{
"firstName": "Test1",
"lastName": "Test1"
}
],
"2018-06-06": [
{
"firstName": "Test1",
"lastName": "Test1"
}
]
}
我试着像这样解析它:
public struct Schedule: Codable {
public let periods: Periods
}
public struct Periods: Codable {
public let unknown: [Inner]
public struct Inner: Codable {
public let firstName: String
public let lastName: String
}
private struct CustomCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CustomCodingKeys.self)
self.unknown = try container.decode([Inner].self, forKey: CustomCodingKeys(stringValue: "2018-06-06")
}
}
但我只能得到一个值(2018-06-06) 的结果。我在这里有多个要解析的日期。这可能吗?
【问题讨论】:
-
你可以手动解析
-
或者我使用app.quicktype.io快速生成我的结构体