我正在尝试为返回的Financial API做可编码的结构
"Time Series (Daily)": {
"2018-11-16": {
"1. open": "190.5000",
"2. high": "194.9695",
"3. low": "189.4600",
"4. close": "193.5300",
"5. volume": "36928253"
},
"2018-11-15": {
"1. open": "188.3900",
"2. high": "191.9700",
"3. low": "186.9000",
"4. close": "191.4100",
"5. volume": "46478801"
},
"2018-11-14": {
"1. open": "193.9000",
"2. high": "194.4800",
"3. low": "185.9300",
"4. close": "186.8000",
"5. volume": "60800957"
}
}
我不能使用CodingKeys,因为这些键是日期,并且它们各不相同。不确定如何解决这个问题。
答案 0 :(得分:1)
您可以尝试
struct Root: Codable {
let timeSeriesDaily: [String: TimeSeriesDaily] // make key as String to generalize all keys
enum CodingKeys: String, CodingKey {
case timeSeriesDaily = "Time Series (Daily)"
}
}
struct TimeSeriesDaily: Codable {
let the1Open, the2High, the3Low, the4Close,the5Volume: String
enum CodingKeys: String, CodingKey {
case the1Open = "1. open"
case the2High = "2. high"
case the3Low = "3. low"
case the4Close = "4. close"
case the5Volume = "5. volume"
}
}