gpt4 book ai didi

ios - 如何解码包含日期信息的 JSON?

转载 作者:行者123 更新时间:2023-11-29 05:11:24 24 4
gpt4 key购买 nike

我的 JSON 结构为:

"symbol": "SNAP",

"stock_exchange_short": "NYSE",
"timezone_name": "America/New_York",
"intraday": {

"2019-12-20 15:59:00": {
"open": "15.55",
"close": "15.55",
"high": "15.55",
"low": "15.52",
"volume": "670218"
},
"2019-12-20 15:58:00": {
"open": "15.55",
"close": "15.55",
"high": "15.55",
"low": "15.54",
"volume": "284225"
},
...
}

关于如何实现日期信息的任何想法我尝试像这样解析它:

public struct WorldTradingIntradayData: Decodable {
let symbol : String
let stock_exchange_short : String
let timezone_name : String
let intraday : Intraday?
}
public struct Intraday: Decodable {
let dateTime: WorldTradingIntradayDetails

public struct WorldTradingIntradayDetails: Decodable {
let open : String
let close : String
let high : String
let low : String
let volume : String
}

但它没有按预期工作。知道如何做到这一点吗?

最佳答案

我认为它应该被视为字符串,那么如果你想将其转换为日期,你需要更改空格的T来创建dateFormatter:

struct WorldTradingIntradayData: Decodable {
let symbol: String
let stockExchangeShort: String
let timezoneName: String
let intraday: [String: Intraday]

enum CodingKeys: String, CodingKey {
case symbol
case stockExchangeShort = "stock_exchange_short"
case timezoneName = "timezone_name"
case intraday
}
}

struct Intraday: Decodable {
let intradayOpen: String
let close: String
let high: String
let low: String
let volume: String

enum CodingKeys: String, CodingKey {
case intradayOpen = "open"
case close
case high
case low
case volume
}
}

关于ios - 如何解码包含日期信息的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59644157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com