将包含字符串键的 JSON 文件解码为 [(Date,Double)],每个字符串键包含一个键值对
Decode a JSON file that contains string keys each of which contains one key value pair into [(Date,Double)]
我正在尝试确定如何读取 JSON 文件,该文件由一系列字符串组成,每个字符串下面是一个键值对到 [(Date,Double)] 中。我已经能够通过手动将主键“Time Series (Daily)”添加到 JSON 文件的顶部和 return [(Date,Double)] 下面的结构来做到这一点。我希望能够消除将“时间序列(每日)”添加到 JSON 文件但仍然 return [(Date,Double)] 的步骤。任何关于如何实现这些结果的见解都将不胜感激。
{
"Time Series (Daily)": { // this entire line is manually added to JSON file
"20200803": {
"NAV": 173.94769
},
"20200804": {
"NAV": 174.57441
},
struct PrincipalTimeSeriesData {
var timeSeriesDaily: [(Date, Double)]
}
extension PrincipalTimeSeriesData: Decodable {
enum CodingKeys: String, CodingKey {
case timeSeriesDaily = "Time Series (Daily)"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
timeSeriesDaily = try container
.decode([String:PrincipalTimeSeriesDaily].self, forKey: .timeSeriesDaily)
.map { (dateFormatterPrin.date(from: [=11=])!, .close) }
}
}
struct PrincipalTimeSeriesDaily {
let close: Double
}
extension PrincipalTimeSeriesDaily: Decodable {
enum CodingKeys: String, CodingKey {
case close = "NAV"
}
}
您可以将数据解码为 [String:PrincipalTimeSeriesDaily]
,然后将生成的字典的 keys/values 映射到您想要的格式:
let jsonData = """
{
"20200803": {
"NAV": 173.94769
},
"20200804": {
"NAV": 174.57441
},
}
""".data(using: .utf8)!
let dateFormatterPrin = DateFormatter()
dateFormatterPrin.dateFormat = "yyyyMMdd"
struct PrincipalTimeSeriesDaily {
let close: Double
}
extension PrincipalTimeSeriesDaily: Decodable {
enum CodingKeys: String, CodingKey {
case close = "NAV"
}
}
do {
let decoded = try JSONDecoder().decode([String:PrincipalTimeSeriesDaily].self, from: jsonData)
let converedToDateKeysArray = decoded.map { item -> (Date,Double) in
(dateFormatterPrin.date(from: item.key)!,item.value.close)
}.sorted { [=10=].0 < .0 }
print(converedToDateKeysArray)
} catch {
print(error)
}
我正在尝试确定如何读取 JSON 文件,该文件由一系列字符串组成,每个字符串下面是一个键值对到 [(Date,Double)] 中。我已经能够通过手动将主键“Time Series (Daily)”添加到 JSON 文件的顶部和 return [(Date,Double)] 下面的结构来做到这一点。我希望能够消除将“时间序列(每日)”添加到 JSON 文件但仍然 return [(Date,Double)] 的步骤。任何关于如何实现这些结果的见解都将不胜感激。
{
"Time Series (Daily)": { // this entire line is manually added to JSON file
"20200803": {
"NAV": 173.94769
},
"20200804": {
"NAV": 174.57441
},
struct PrincipalTimeSeriesData {
var timeSeriesDaily: [(Date, Double)]
}
extension PrincipalTimeSeriesData: Decodable {
enum CodingKeys: String, CodingKey {
case timeSeriesDaily = "Time Series (Daily)"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
timeSeriesDaily = try container
.decode([String:PrincipalTimeSeriesDaily].self, forKey: .timeSeriesDaily)
.map { (dateFormatterPrin.date(from: [=11=])!, .close) }
}
}
struct PrincipalTimeSeriesDaily {
let close: Double
}
extension PrincipalTimeSeriesDaily: Decodable {
enum CodingKeys: String, CodingKey {
case close = "NAV"
}
}
您可以将数据解码为 [String:PrincipalTimeSeriesDaily]
,然后将生成的字典的 keys/values 映射到您想要的格式:
let jsonData = """
{
"20200803": {
"NAV": 173.94769
},
"20200804": {
"NAV": 174.57441
},
}
""".data(using: .utf8)!
let dateFormatterPrin = DateFormatter()
dateFormatterPrin.dateFormat = "yyyyMMdd"
struct PrincipalTimeSeriesDaily {
let close: Double
}
extension PrincipalTimeSeriesDaily: Decodable {
enum CodingKeys: String, CodingKey {
case close = "NAV"
}
}
do {
let decoded = try JSONDecoder().decode([String:PrincipalTimeSeriesDaily].self, from: jsonData)
let converedToDateKeysArray = decoded.map { item -> (Date,Double) in
(dateFormatterPrin.date(from: item.key)!,item.value.close)
}.sorted { [=10=].0 < .0 }
print(converedToDateKeysArray)
} catch {
print(error)
}