期望解码 Array<Any> 但发现了字典

Expected to decode Array<Any> but found a dictionary instead

我正在从返回数组的 API 中获取数据,但需要将其替换为具有 "sub levels":

的 API
RAW:
    ETH:
        USD:
             TYPE:              "5"
             MARKET:            "CCCAGG"
             FROMSYMBOL:        "ETH"
             TOSYMBOL:          "USD"
             PRICE:             680.89
             CHANGEPCT24HOUR    :   -9.313816893529749

这是我的结构:

struct Ethereum: Codable {

    let percentChange24h: String
    let priceUSD: String

    private enum CodingKeys: String, CodingKey {
        case priceUSD = "PRICE", percentChange24h = "CHANGEPCT24HOUR"
    }
}

和实施:

    func fetchEthereumInfo(completion: @escaping (Ethereum?, Error?) -> Void) {
    let url = URL(string: "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD")!

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let data = data else { return }
        do {
            if let ethereumUSD = try JSONDecoder().decode([Ethereum].self, from: data).first {
                print(ethereumUSD)
                completion(ethereumUSD, nil)
            }
        } catch {
            print(error)
        }
    }
    task.resume()
}

控制台打印typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

我真的不知道要在我的代码中更新什么,或者这种形式的 API 是什么

首先JSON不包含任何数组。非常非常容易阅读 JSON。只有 2(两个!)集合类型,数组 [] 和字典 {}。如您所见,JSON 字符串中根本没有方括号。

任何(子)词典 {} 都必须解码为它自己的类型,所以它应该是

struct Root : Decodable {
    private enum CodingKeys : String, CodingKey { case raw = "RAW" }
    let raw : RAW
}

struct RAW : Decodable {
    private enum CodingKeys : String, CodingKey { case eth = "ETH" }
    let eth : ETH
}

struct ETH : Decodable {
    private enum CodingKeys : String, CodingKey { case usd = "USD" }
    let usd : USD
}

struct USD : Decodable {

    private enum CodingKeys : String, CodingKey {
        case type = "TYPE"
        case market = "MARKET"
        case price = "PRICE"
        case percentChange24h = "CHANGEPCT24HOUR"
    }
    let type : String
    let market : String
    let price : Double
    let percentChange24h : Double
}

要解码 JSON 并打印 percentChange24h 你必须写

 let result = try JSONDecoder().decode(Root.self, from: data)
 print("percentChange24h", result.raw.eth.usd.percentChange24h)