使用Codable协议解析包含日期作为关键字的Stock API [英] Parsing a Stock API that contain dates as keys using the Codable protocol

查看:52
本文介绍了使用Codable协议解析包含日期作为关键字的Stock API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Alpha Vantage解析股票api. 响应如下所示: API响应演示

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:

  1. 股票
  2. 元数据
  3. 时间序列
  4. 打开高位低位关闭

我认为问题出在时间序列类上,因为我想将一个数组日期作为键,每个键都包含打开,关闭,高,低值.这三个类都符合Codable协议.我更改了枚举中​​键的值,以便它可以与正确的JSON Response匹配.

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"
}
}

推荐答案

以下是您的正确数据类型

Here are the proper data types w.r.t your response

// MARK: - Response
struct Response: Codable {
    let metaData: MetaData
    let timeSeries5Min: [String: TimeSeries5Min]

    enum CodingKeys: String, CodingKey {
        case metaData = "Meta Data"
        case timeSeries5Min = "Time Series (5min)"
    }
}

// MARK: - MetaData
struct MetaData: Codable {
    let the1Information, the2Symbol, the3LastRefreshed, the4Interval: String
    let the5OutputSize, the6TimeZone: String

    enum CodingKeys: String, CodingKey {
        case the1Information = "1. Information"
        case the2Symbol = "2. Symbol"
        case the3LastRefreshed = "3. Last Refreshed"
        case the4Interval = "4. Interval"
        case the5OutputSize = "5. Output Size"
        case the6TimeZone = "6. Time Zone"
    }
}

// MARK: - TimeSeries5Min
struct TimeSeries5Min: 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"
    }
}

用法:

  func loadURL() {
    let stocksURL = URL(string: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo")
    URLSession.shared.dataTask(with: stocksURL!) { (data, response, error) in
      if let error = error {
        print(error)
        return
      }
      do {
        let response = try JSONDecoder().decode(Response.self, from: data!)
        response.timeSeries5Min.forEach({ (keyValue) in
          print(keyValue)
        })
      } catch {
        print(error)
      }
    }.resume()
  }

这篇关于使用Codable协议解析包含日期作为关键字的Stock API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆