我正在尝试从我创建的 API 中提取数据。在我开始解析/解码数据之前,一切似乎都很顺利。
这些是出现的错误:
这是我的代码
import Foundation
struct VaccineManager {
let vaccineURL = "https://coronavirus.data.gov.uk/api/v1/data?filters=areaType=overview&structure=%7B%22areaType%22:%22areaType%22,%22areaName%22:%22areaName%22,%22areaCode%22:%22areaCode%22,%22date%22:%22date%22,%22newPeopleVaccinatedFirstDoseByPublishDate%22:%22newPeopleVaccinatedFirstDoseByPublishDate%22,%22newPeopleVaccinatedSecondDoseByPublishDate%22:%22newPeopleVaccinatedSecondDoseByPublishDate%22,%22cumPeopleVaccinatedFirstDoseByPublishDate%22:%22cumPeopleVaccinatedFirstDoseByPublishDate%22,%22cumPeopleVaccinatedSecondDoseByPublishDate%22:%22cumPeopleVaccinatedSecondDoseByPublishDate%22%7D&format=json"
func performRequest(vaccineURL: String){
if let url = URL(string: vaccineURL) {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
parseJASON(vaccineData: safeData)
}
}
task.resume()
}
}
func parseJASON(vaccineData: Data) {
let decoder = JSONDecoder()
do{
let decodedData = try decoder.decode(VaccineData.self, from: vaccineData)
print(decodedData.data[0].date)
} catch {
print(error)
}
}
万一这与它有关,我也有一个包含此代码的文件
struct VaccineData: Decodable {
let data: Data
}
struct Data: Decodable {
let newPeopleVaccinatedFirstDoseByPublishDate: Int
}
卡了好几天了,谢谢你的帮助!