预期解码 Array<Any> 但发现了字典。阅读有问题 JSON
Expected to decode Array<Any> but found a dictionary instead. Problem reading JSON
我知道有一些类似的问题,但它的 JSON 无法与我的进行比较。
我的 JSON:
https://www.cbr-xml-daily.ru/daily_json.js
我的代码:
struct CoinData: Decodable {
let Valute: [Valute]
}
struct Valute: Decodable {
let Name: String
let Value: Double
}
if let safeData = data {
if let coinData = self.parseJSON(safeData) {
print(coinData) // I expect a list of all the currencies to be printed here
}
}
func parseJSON(_ data: Data) -> [Valute]? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(CoinData.self, from: data)
let coinsList = decodedData.Valute
return coinsList
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
我的错误:
"Expected to decode Array<Any> but found a dictionary instead."
应该改变什么?
照上面写的做:“错误...找到字典”-> 按字典解码:
struct CoinData: Decodable {
let valute: [String: Valute]
}
struct Valute: Decodable {
let name: String
let value: Double
}
我知道有一些类似的问题,但它的 JSON 无法与我的进行比较。
我的 JSON:
https://www.cbr-xml-daily.ru/daily_json.js
我的代码:
struct CoinData: Decodable {
let Valute: [Valute]
}
struct Valute: Decodable {
let Name: String
let Value: Double
}
if let safeData = data {
if let coinData = self.parseJSON(safeData) {
print(coinData) // I expect a list of all the currencies to be printed here
}
}
func parseJSON(_ data: Data) -> [Valute]? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(CoinData.self, from: data)
let coinsList = decodedData.Valute
return coinsList
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
我的错误:
"Expected to decode Array<Any> but found a dictionary instead."
应该改变什么?
照上面写的做:“错误...找到字典”-> 按字典解码:
struct CoinData: Decodable {
let valute: [String: Valute]
}
struct Valute: Decodable {
let name: String
let value: Double
}