如何使用 Swift 中的对象数组解析 JSON 文件?
How do I parse JSON file with an array of objects in Swift?
几个小时以来,我一直在尝试使用数组解析 JSON 文件以试验 API,但我不断收到错误。我设法只用一个 JSON object/dictionary 解析一个文件(按照不同的教程),但我无法弄清楚一个包含对象数组的 JSON 文件。我正在用冠状病毒 API 对此进行测试。这是 URL,因此您可以看到 JSON 代码:https://coronavirus-19-api.herokuapp.com/countries。我觉得解决方案非常简单,只是缺少一些小东西,但我不太确定。
这是我得到的错误:
valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 10", intValue: 10), CodingKeys(stringValue: "recovered", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))
这是我的代码:
import UIKit
class FirstViewController: UIViewController {
var coronaInfo = [CoronaInfo]()
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://coronavirus-19-api.herokuapp.com/countries"
getData(from: url) { (data) in
self.coronaInfo = data
print(self.coronaInfo[0].cases) // <-- Works in here
}
print(coronaInfo[0].cases) // <-- Outside of the closure it's nil
}
func getData(from url: String, completed: @escaping ([CoronaInfo]) -> ()) {
URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
// Make sure that data isn't nil and that there was no error
guard let data = data, error == nil else { print("Something went wrong"); return }
var result: [CoronaInfo]?
do {
// Convert data from bytes to the custom object (Known as JSON decoding)
result = try JSONDecoder().decode([CoronaInfo].self, from: data)
guard let json = result else { return }
DispatchQueue.main.async {
completed(json)
}
} catch { print(error) }
}.resume()
}
}
struct CoronaInfo: Codable {
let country: String
let cases: Int
let todayCases: Int
let deaths: Int
let todayDeaths: Int
let recovered: Int?
let active: Int?
let critical: Int
let casesPerOneMillion: Int
let deathsPerOneMillion: Int
let totalTests: Int
let testsPerOneMillion: Int
}
在此先感谢您的帮助!
这里有两点值得一提
- 您尝试解析的响应应该来自 https://coronavirus-19-api.herokuapp.com/countries not from https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats。所以使用第一个 link 而不是第二个
let url = "https://coronavirus-19-api.herokuapp.com/countries"
getData(from: url)
- 由于有两个字段具有
null
值,请在您的模型中将它们标记为可选。
struct CoronaData: Codable {
let country: String
let cases: Int
let todayCases: Int
let deaths: Int
let todayDeaths: Int
let recovered: Int?
let active: Int?
let critical: Int
let casesPerOneMillion: Int
let deathsPerOneMillion: Int
let totalTests: Int
let testsPerOneMillion: Int
}
几个小时以来,我一直在尝试使用数组解析 JSON 文件以试验 API,但我不断收到错误。我设法只用一个 JSON object/dictionary 解析一个文件(按照不同的教程),但我无法弄清楚一个包含对象数组的 JSON 文件。我正在用冠状病毒 API 对此进行测试。这是 URL,因此您可以看到 JSON 代码:https://coronavirus-19-api.herokuapp.com/countries。我觉得解决方案非常简单,只是缺少一些小东西,但我不太确定。
这是我得到的错误:
valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 10", intValue: 10), CodingKeys(stringValue: "recovered", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))
这是我的代码:
import UIKit
class FirstViewController: UIViewController {
var coronaInfo = [CoronaInfo]()
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://coronavirus-19-api.herokuapp.com/countries"
getData(from: url) { (data) in
self.coronaInfo = data
print(self.coronaInfo[0].cases) // <-- Works in here
}
print(coronaInfo[0].cases) // <-- Outside of the closure it's nil
}
func getData(from url: String, completed: @escaping ([CoronaInfo]) -> ()) {
URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
// Make sure that data isn't nil and that there was no error
guard let data = data, error == nil else { print("Something went wrong"); return }
var result: [CoronaInfo]?
do {
// Convert data from bytes to the custom object (Known as JSON decoding)
result = try JSONDecoder().decode([CoronaInfo].self, from: data)
guard let json = result else { return }
DispatchQueue.main.async {
completed(json)
}
} catch { print(error) }
}.resume()
}
}
struct CoronaInfo: Codable {
let country: String
let cases: Int
let todayCases: Int
let deaths: Int
let todayDeaths: Int
let recovered: Int?
let active: Int?
let critical: Int
let casesPerOneMillion: Int
let deathsPerOneMillion: Int
let totalTests: Int
let testsPerOneMillion: Int
}
在此先感谢您的帮助!
这里有两点值得一提
- 您尝试解析的响应应该来自 https://coronavirus-19-api.herokuapp.com/countries not from https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats。所以使用第一个 link 而不是第二个
let url = "https://coronavirus-19-api.herokuapp.com/countries"
getData(from: url)
- 由于有两个字段具有
null
值,请在您的模型中将它们标记为可选。
struct CoronaData: Codable {
let country: String
let cases: Int
let todayCases: Int
let deaths: Int
let todayDeaths: Int
let recovered: Int?
let active: Int?
let critical: Int
let casesPerOneMillion: Int
let deathsPerOneMillion: Int
let totalTests: Int
let testsPerOneMillion: Int
}