mr.a
mr.a

Reputation: 3

Swift JSON decoding error: "Expected to decode Dictionary<String, Any> but found an array instead."

I am having trouble decoding the following JSON (which is entirely enclosed in an array):

[
  {
    "symbol": "AAPL",
    "name": "Apple Inc.",
    "price": 254.81,
    "changesPercentage": 2.85,
    "change": 7.07,
    "dayLow": 249.4,
    "dayHigh": 255.5,
    "yearHigh": 327.85,
    "yearLow": 170.27,
    "marketCap": 1114915995648,
    "priceAvg50": 281.9003,
    "priceAvg200": 270.52628,
    "volume": 37900589,
    "avgVolume": 48846970,
    "exhange": "NASDAQ",
    "open": 250.74,
    "previousClose": 247.74,
    "eps": 12.595,
    "pe": 20.231043,
    "earningsAnnouncement": "2020-01-28T21:30:00.000+0000",
    "sharesOutstanding": 4375479808,
    "timestamp": 1585600706
  }
]

This is the Struct I am using to decode:

struct StockData: Codable {

    let name: String
    let exchange: String
    let price: Double
    let yearLow: Double
    let yearHigh: Double
    let marketCap: Double
}

Here is my code to decode it:

func parseJSON(_ stockData: Data) -> StockModel? {
        let decoder = JSONDecoder()
        do{
            let decodedData = try decoder.decode(StockData.self, from: stockData)

                let name = decodedData.name
                let exchange = decodedData.exchange
                let price = String(decodedData.price)
                let yearLow = String(decodedData.yearLow)
                let yearHigh = String(decodedData.yearHigh)
                let marketCap = String(decodedData.marketCap)

            let stockModel = StockModel(name: name, exchange: exchange, stockAttributes: [price, marketCap, yearLow, yearHigh])

            return stockModel

        }catch{
            print(error)
           return nil
        }
    }

I have tried making it: try decoder.decode([StockData].self, from: stockData)so that stockData is enclosed in brackets, indicating that it is an array, but I get errors

How do I decode JSON that is entirely enclosed in an Array?

Upvotes: 0

Views: 129

Answers (1)

vadian
vadian

Reputation: 285079

[StockData].self is right. The error (and the JSON) clearly states that the root object is an array.

To extract the data get the first item of the array

func parseJSON(_ stockData: Data) -> StockModel? {
    let decoder = JSONDecoder()
    do{
        let decodedData = try decoder.decode([StockData].self, from: stockData)
        guard let stock = decodedData.first else { return nil }
        let name = stock.name
        let exchange = stock.exchange
        let price = String(stock.price)
        let yearLow = String(stock.yearLow)
        let yearHigh = String(stock.yearHigh)
        let marketCap = String(stock.marketCap)

        let stockModel = StockModel(name: name, exchange: exchange, stockAttributes: [price, marketCap, yearLow, yearHigh])

        return stockModel

    } catch {
        print(error)
        return nil
    }
}

Please read the Swift Language Guide

Upvotes: 3

Related Questions