问题描述
我知道也有类似的问题,但是它的JSON无法与我的JSON进行比较。
我的JSON:
https://www.cbr-xml-daily.ru/daily_json.js
我的代码:
['bonjour','Hola']
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
}
}
应该更改什么?
解决方法
执行它写的内容:“错误...找到字典”->按字典解码:
...
created() {
this.$store.dispatch('inijson')
},...