I am trying to parse a stock api from Alpha Vantage
.
This is what a response looks like: API Response Demo
I set up Four classes to use for decoding and encoding:
- Stocks
- Meta Data
- Time Series
- Open High Low Close
I think the problem lies in the Time Series Classes since i am suppose to be getting an array dates as a Key, which each contain Open, Close, High, Low values. All three classes conform to the Codable
protocol.I changed the value of the keys in the enum so that it could match the correct JSON Response
.
var stocks = [Stocks]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
loadURL()
}
func loadURL(){
let stocksURL = URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo")
print("Inside loadURL")
guard let url = stocksURL else { return }
if let data = try? Data(contentsOf: url){
print("Inside data")
parse(json: data)
}
}
func parse(json: Data){
let jsonDecoder = JSONDecoder()
print("before Decoding")
if let json = try? jsonDecoder.decode([Stocks].self, from: json){
print("Inside parsing")
stocks = json
print(stocks[0].meta_data.symbol)
print(stocks[0].time_series.OHLC[0].open)
}
}
}
class Stocks: Codable {
var meta_data: MetaData
var time_series: TimeSeries
enum CodingKeys: String, CodingKey {
case meta_data = "Meta Data"
case time_series = "Time Series (5min)"
}
}
class MetaData: Codable {
var information: String
var symbol: String
var lastRefreshed: String
var outputSize: String
var timeZone: String
enum CodingKeys: String, CodingKey {
case information = "1. Information"
case symbol = "2. Symbol"
case lastRefreshed = "3. Last Refreshed"
case outputSize = "4. Output Size"
case timeZone = "5. Time Zone"
}
}
class TimeSeries: Codable {
var OHLC: [OpenHighLowClose]
}
class OpenHighLowClose: Codable {
var open: Double
var high: Double
var low: Double
var close: Double
var volume: Double
enum CodingKeys: String, CodingKey {
case open = "1. open"
case high = "2. high"
case low = "3. low"
case close = "4. close"
case volume = "5. volume"
}
}
See Question&Answers more detail:os