我想获取一些JSON数据。我做了一个Struct,但是这个Struct似乎不起作用。当我运行代码时,会出现此错误:
typeMismatch(Swift.Int,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“ data”,intValue:nil),CodingKeys(stringValue:“ match”,intValue:nil),_JSONKey(stringValue:“ Index 0“,intValue:0),CodingKeys(stringValue:” time“,intValue:nil)],debugDescription:”预期对Int进行解码,但找到了一个字符串/数据。“,底层错误:nil)))
我是JSON和Struct的新手,所以我不明白自己在做什么错。我在Internet上搜索了类似的问题,但是它们的解决方案似乎对我不起作用。 结构:
struct Data: Codable {
var data: Match
init(data: Match) {
self.data = data
}
}
struct Match: Codable {
var match: [Live_Match]
init(match: [Live_Match]) {
self.match = match
}
}
struct Live_Match: Codable {
let competition_id: Int?
let status: String?
let ht_score: String?
let ft_score: String?
let et_score: String?
let league_name: String?
let away_id: Int?
let score: String?
let competition_name: String?
let home_id: Int?
let away_name: String?
let time: Int?
let home_name: String?
let league_id: Int?
init(competition_id: Int?, status: String?, ht_score: String?, ft_score: String?, et_score: String?, league_name: String?, away_id: Int?, score: String?, competition_name: String?, home_id: Int?, away_name: String?, time: Int?, home_name: String?, league_id: Int?) {
self.competition_id = competition_id
self.status = status
self.ht_score = ht_score
self.ft_score = ft_score
self.et_score = et_score
self.league_name = league_name
self.away_id = away_id
self.score = score
self.competition_name = competition_name
self.home_id = home_id
self.away_name = away_name
self.time = time
self.home_name = home_name
self.league_id = league_id
}
}
获取功能:
final let url_live = URL(string:"http://livescore-api.com/api-client/scores/live.json?key=6iNDEUYbh6NZHBdG&secret=RxkjBBp4h7MoKw2ROQvtLtE3zs2h4fXV&country=2")
private var Live_Matches = [Live_Match]()
func downloadJson() {
guard let downloadURL = url_live else { return }
URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in
guard let data = data, error == nil, urlResponse != nil else {
print(error?.localizedDescription)
return
}
print("downloaded")
do
{
let decoder = JSONDecoder()
let downloadedLive_Matches = try decoder.decode(Data.self, from: data)
print(downloadedLive_Matches)
self.Live_Matches = downloadedLive_Matches.data.match
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print(error.localizedDescription)
}
}.resume()
}
JSON:
{
"success": true,
"data": {
"match": [
{
"competition_id": 68,
"status": "HALF TIME BREAK",
"ht_score": "0 - 0",
"ft_score": "",
"et_score": "",
"last_changed": "2019-07-28 16:52:14",
"id": 151050,
"league_name": "First Division A",
"away_id": 806,
"score": "0 - 0",
"competition_name": "First Division A",
"events": false,
"home_id": 354,
"away_name": "Gent",
"added": "2019-07-28 15:45:20",
"time": "HT",
"home_name": "Sporting Charleroi",
"league_id": 48
}, //And some more data
]
}
}
答案 0 :(得分:0)
错误消息中的编码路径告诉您问题出在哪里(在data
中,在match
中,在第一个数组条目中以及在键time
下)。沿该编码路径到达JSON中的适当位置后,您会看到typeMismatch
问题,即您将time
定义为Int?
,但是JSON具有字符串{ {1}}。