title: How should I go about parsing a JSON response from an API with Alamofire and SwiftyJSON?
body: <p>I am trying to parse a JSON from an API using Alamofire and SwiftyJSON, but am having trouble trying to access the information in the JSON. I need to simply parse the JSON for an item called "ask_price": and also "time_coinapi" but I am not sure how I manage the response, or if I have to use a different method. here is what I have at the moment:</p>
  
  <pre><code>class CoinAPIManager {
  
  var prices: [String] = []
  var times: [String] = []
  
  
  static let shared = CoinAPIManager()
  
  func getReq() {
  
      let headers: HTTPHeaders = [
          "X-CoinAPI-Key": "Key"
      ]
  
  
      Alamofire.request("https://rest.coinapi.io/v1/quotes/BITSTAMP_SPOT_BTC_USD/history?time_start=2018-08-21T00:00:00&amp;time_end=2018-08-22T00:00:00&amp;limit=100", headers: headers).responseJSON { response in
          debugPrint(response)
  
          if let data = try? String(contentsOf: response) {
              let json = JSON(parseJSON: data)
  
  
              parse(json: json)
  
  
          }
  
  
  
  }
  
  
  
      func parse(json: JSON) {
          for result in json[].arrayValue {
              let price = result["ask_price"].stringValue
  
  
          }
  
      }
  
  }
  }
  </code></pre>
  
  <p>and I have also tried this: </p>
  
  <pre><code>func getReq() {
  
      let headers: HTTPHeaders = [
          "X-CoinAPI-Key": "Key"
      ]
  
  
      Alamofire.request("https://rest.coinapi.io/v1/quotes/BITSTAMP_SPOT_BTC_USD/history?time_start=2018-08-21T00:00:00&amp;time_end=2018-08-22T00:00:00&amp;limit=100", headers: headers).responseJSON { response in
          debugPrint(response)
  
          switch response.result {
          case .failure(let error):
              // Do whatever here
              return
  
          case .success(let data):
              // First make sure you got back a dictionary if that's what you expect
              guard let json = data as? [String : AnyObject] else {
                  print("Failed to get expected response from webserver.")
                  return
              }
  
              // Then make sure you get the actual key/value types you expect
              guard var price = json["ask_price"] as? Double else {
                      print("Failed to get data from webserver")
                      return
              }
  
  
  
  }
  </code></pre>
  
  <p>What am I doing wrong? this is how the JSON looks:</p>
  
  <pre><code>[
    {
  "symbol_id": "BITSTAMP_SPOT_BTC_USD",
  "time_exchange": "2013-09-28T22:40:50.0000000Z",
  "time_coinapi": "2017-03-18T22:42:21.3763342Z",
  "ask_price": 770.000000000,
  "ask_size": 3252,
  "bid_price": 760,
  "bid_size": 124
    },
    {
  "symbol_id": "BITSTAMP_SPOT_BTC_USD",
  "time_exchange": "2013-09-28T22:40:50.0000000",
  "time_coinapi": "2017-03-18T22:42:21.3763342",
  "ask_price": 770.000000000,
  "ask_size": 3252,
  "bid_price": 760,
  "bid_size": 124
    }
  ]
  </code></pre>
  
  <p>previous question deleted and reposted due to large mistake</p>