arrays - 使用 Api 解析 Swift 的 Json (alpha vantage)
问题描述
Swift Json 解析
我正在尝试解析 json 以分配值。我是 swift 新手,无法在 YouTube 或网络上找到解决方案。我不确定我是否必须拥有与 json 的“”内部相同的构造,但有一个空格。此外,我不确定日期是否用于调用当天的信息。
struct Stock: Decodable {
let symbol: String
let open: Double
let high: Double
let low: Double
let close: Double
let volume: Int
}
var tag : String = "TSLA"
let url = URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=TSLA&apikey=/(key)")
let session = URLSession.shared
session.dataTask(with: url!) { (data, response, error) in
// pull what is coming from the interent
if let response = response {
print(response)
}
// print the data but it wont be unpacked
if let data = data {
print(data)
}
// json serialization
do{
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
let stock = try
JSONDecoder().decode(Stock.self, from: data!)
print(Stock.init(symbol: String, open: Double, high: Double, low: Double, close: Double, volume: Int))
} catch {
print(error)
}
}.resume()
打印 json 的示例:
"Meta Data" = {
"1. Information" = "Daily Prices (open, high, low, close) and Volumes";
"2. Symbol" = TSLA;
"3. Last Refreshed" = "2020-06-01";
"4. Output Size" = Compact;
"5. Time Zone" = "US/Eastern";
};
"Time Series (Daily)" = {
"2020-01-08" = {
"1. open" = "473.7000";
"2. high" = "498.4900";
"3. low" = "468.2300";
"4. close" = "492.1400";
"5. volume" = 31199393;
};
"2020-01-09" = {
"1. open" = "497.1000";
"2. high" = "498.8000";
"3. low" = "472.8700";
"4. close" = "481.3400";
"5. volume" = 28463186;
};
"2020-01-10" = {
"1. open" = "481.7900";
"2. high" = "484.9400";
"3. low" = "473.7000";
"4. close" = "478.1500";
"5. volume" = 12976832;
解决方案
您需要像这样修改结构并解码如下:-
结构:
struct Stock: Codable {
let metaData: MetaData
let timeSeriesDaily: [String: TimeSeriesDaily]
enum CodingKeys: String, CodingKey {
case metaData = "Meta Data"
case timeSeriesDaily = "Time Series (Daily)"
}
}
struct MetaData: Codable {
let the1Information, the2Symbol, the3LastRefreshed, the4OutputSize: String
let the5TimeZone: String
enum CodingKeys: String, CodingKey {
case the1Information = "1. Information"
case the2Symbol = "2. Symbol"
case the3LastRefreshed = "3. Last Refreshed"
case the4OutputSize = "4. Output Size"
case the5TimeZone = "5. Time Zone"
}
}
struct TimeSeriesDaily: Codable {
let the1Open, the2High, the3Low, the4Close: String
let 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"
}
}
网址会话:
URLSession.shared.dataTask(with: URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=TSLA&apikey=TSLA")!) { (data, _, _) in
let stock = try! JSONDecoder().decode(Stock.self, from: data!)
print(stock)
}.resume()
注意:这只是一个示例,您应该自己处理错误。
推荐阅读
- batch-file - 如何获得具有绝对路径但具有特定名称规则的列表?(批处理脚本)
- ruby - each_with_index 重复记录
- javascript - 如果 div 包含某些内容,则更改父 div 的 css
- python - 运行 VSCode nodebug 时,当前目录未添加到 sys.path
- forms - Symfony 形式的值对象约束
- mongodb - MongoTemplate 和/或 MongoRepository 是否支持 Mongo 4 事务?
- python - 如何使用 pandas 读取 .txt 数据列并提供给 TF
- spring - 使用 JAXB 编组 XML 元素内的属性
- javascript - 循环结果数组的最后一个索引
- java - EnumSet.spliterator 没有特征 Spliterator.NONNULL