简体   繁体   中英

Swift Decoding nested JSON

I have a problem with parsing data from NBP api " http://api.nbp.pl/api/exchangerates/tables/a/?format=json " . I created struct CurrencyDataStore and Currency

struct CurrencyDataStore: Codable {
var table: String
var no : String
var rates: [Currency]

enum CodingKeys: String, CodingKey {
    case table
    case no
    case rates
}

init(from decoder: Decoder) throws {

    let values = try decoder.container(keyedBy: CodingKeys.self)

    table = ((try values.decodeIfPresent(String.self, forKey: .table)))!
    no = (try values.decodeIfPresent(String.self, forKey: .no))!
    rates = (try values.decodeIfPresent([Currency].self, forKey: .rates))!
} }

struct Currency: Codable {
var currency: String
var code: String
var mid: Double

enum CodingKeys: String, CodingKey {
    case currency
    case code
    case mid
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)

    currency = try values.decode(String.self, forKey: .currency)
    code = try values.decode(String.self, forKey: .code)
    mid = try values.decode(Double.self, forKey: .mid) 
}
}

In controllerView class I wrote 2 methods to parse data from API

func getLatestRates(){
    guard let currencyUrl = URL(string: nbpApiUrl) else {
        return
    }

    let request = URLRequest(url: currencyUrl)
    let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in

        if let error = error {
            print(error)
            return
        }

        if let data = data {
            self.currencies = self.parseJsonData(data: data)
        }
    })

    task.resume()
}

func parseJsonData(data: Data) -> [Currency] {

    let decoder = JSONDecoder()

    do{
        let currencies = try decoder.decode([String:CurrencyDataStore].self, from: data)

    }
    catch {
        print(error)
    }
    return currencies
}

This code didn't work. I have this error "typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary but found an array instead.", underlyingError: nil))".

Could you help me?

The JSON being returned by that API gives you an array, not a dictionary, but you're telling the JSONDecoder to expect a dictionary type. Change that line to:

let currencies = try decoder.decode([CurrencyDataStore].self, from: data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM