Swift: 未找到密钥 JSON 解码
Swift: key not found JSON decoding
我正在尝试从这个网站解码 JSON 对象:https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer
我想将它们存储在一个足球元素数组中,并在单元格中显示它们。
这是我做的代码,但是我有找不到密钥的错误,这怎么可能?
class Soccer: Codable {
var strLeague: String
var strDescriptionEN: String
var strBadge: String
var strDivision: String
var intFormedYear: String
var strCountry: String
init(strLeague: String, strDescriptionEN: String, strBadge: String, strDivision: String, intFormedYear: String, strCountry: String) {
self.strLeague = strLeague
self.strDescriptionEN = strDescriptionEN
self.strBadge = strBadge
self.strDivision = strDivision
self.intFormedYear = intFormedYear
self.strCountry = strCountry
}
}
class SoccerTVC: UITableViewController {
var leagues = [Soccer]()
func download(at url: String, handler: @escaping (Data?) -> Void)
{
// 1 - Create URL
guard let url = URL(string: url) else {
debugPrint("Failed to create URL")
handler(nil)
return
}
// 2 - Create GET Request
var request: URLRequest = URLRequest(url: url)
request.httpMethod = "GET"
// 3 - Create download task, handler will be called when request ended
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in handler(data)
}
task.resume()
}
func getSoccer() {
// 1 - Download Soccer
download(at: "https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer")
{ (SoccerData) in
if let Soccerdata = SoccerData {
// 2 - Decode JSON into a array of Game object
let decoder: JSONDecoder = JSONDecoder()
do {
let jsonData = [try decoder.decode(Soccer.self, from: Soccerdata)]
self.leagues = jsonData
debugPrint(self.leagues)
DispatchQueue.main.sync {
self.tableView.reloadData()
}
}
catch {
debugPrint("Failed to parse data - error: \(error)")
}
}
else
{
debugPrint("Failed to get soccer data")
}
}
}
override func viewDidLoad() {
getSoccer()
super.viewDidLoad()
}
}
错误信息:
Failed to parse data - error: keyNotFound(CodingKeys(stringValue:
"strLeague", intValue: nil), Swift.DecodingError.Context(codingPath:
[], debugDescription: "No value associated with key
CodingKeys(stringValue: \"strLeague\", intValue: nil)
(\"strLeague\").", underlyingError: nil))
您想要的值在 country
数组键中,因此您需要更改结构:
struct Soccer: Codable {
let countrys : [Country]?
}
struct Country : Codable{
var strLeague: String
var strDescriptionEN: String
var strBadge: String
....
}
并且您需要像这样更改 json 解码器:
var leagues = [Country]()
let jsonData = try decoder.decode(Soccer.self, from: Soccerdata)
self.leagues = jsonData.countrys
}
试试这个:
let jsonData = try JSONDecoder().decode([String:[Soccer]].self, from: Soccerdata)
self.leagues = jsonData.values
我正在尝试从这个网站解码 JSON 对象:https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer
我想将它们存储在一个足球元素数组中,并在单元格中显示它们。
这是我做的代码,但是我有找不到密钥的错误,这怎么可能?
class Soccer: Codable {
var strLeague: String
var strDescriptionEN: String
var strBadge: String
var strDivision: String
var intFormedYear: String
var strCountry: String
init(strLeague: String, strDescriptionEN: String, strBadge: String, strDivision: String, intFormedYear: String, strCountry: String) {
self.strLeague = strLeague
self.strDescriptionEN = strDescriptionEN
self.strBadge = strBadge
self.strDivision = strDivision
self.intFormedYear = intFormedYear
self.strCountry = strCountry
}
}
class SoccerTVC: UITableViewController {
var leagues = [Soccer]()
func download(at url: String, handler: @escaping (Data?) -> Void)
{
// 1 - Create URL
guard let url = URL(string: url) else {
debugPrint("Failed to create URL")
handler(nil)
return
}
// 2 - Create GET Request
var request: URLRequest = URLRequest(url: url)
request.httpMethod = "GET"
// 3 - Create download task, handler will be called when request ended
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in handler(data)
}
task.resume()
}
func getSoccer() {
// 1 - Download Soccer
download(at: "https://www.thesportsdb.com/api/v1/json/1/search_all_leagues.php?c=France&s=Soccer")
{ (SoccerData) in
if let Soccerdata = SoccerData {
// 2 - Decode JSON into a array of Game object
let decoder: JSONDecoder = JSONDecoder()
do {
let jsonData = [try decoder.decode(Soccer.self, from: Soccerdata)]
self.leagues = jsonData
debugPrint(self.leagues)
DispatchQueue.main.sync {
self.tableView.reloadData()
}
}
catch {
debugPrint("Failed to parse data - error: \(error)")
}
}
else
{
debugPrint("Failed to get soccer data")
}
}
}
override func viewDidLoad() {
getSoccer()
super.viewDidLoad()
}
}
错误信息:
Failed to parse data - error: keyNotFound(CodingKeys(stringValue: "strLeague", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"strLeague\", intValue: nil) (\"strLeague\").", underlyingError: nil))
您想要的值在 country
数组键中,因此您需要更改结构:
struct Soccer: Codable {
let countrys : [Country]?
}
struct Country : Codable{
var strLeague: String
var strDescriptionEN: String
var strBadge: String
....
}
并且您需要像这样更改 json 解码器:
var leagues = [Country]()
let jsonData = try decoder.decode(Soccer.self, from: Soccerdata)
self.leagues = jsonData.countrys
}
试试这个:
let jsonData = try JSONDecoder().decode([String:[Soccer]].self, from: Soccerdata)
self.leagues = jsonData.values