but found a dictionary instead.", 底层错误:无)-6ren"> but found a dictionary instead.", 底层错误:无)-我想将一个在线 json 文件加载到我的应用程序中,但我遇到了这个错误: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPat-6ren">
gpt4 book ai didi

json - debugDescription : "Expected to decode Array but found a dictionary instead.", 底层错误:无)

转载 作者:行者123 更新时间:2023-12-01 07:51:14 33 4
gpt4 key购买 nike

我想将一个在线 json 文件加载到我的应用程序中,但我遇到了这个错误:

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))



我看过stackoverflow,但其他解决方案并没有帮助解决我的问题。

我的 json

我的数据模型:
import Foundation

struct Initial: Codable {
let copyright: String
let totalItems: Int
let totalEvents: Int
let totalGames: Int
let totalMatches: Int
let wait: Int
let dates: [Dates]
}

struct Dates: Codable {
let date: String
let totalItems: Int
let totalEvents: Int
let totalGames: Int
let totalMatches: Int
let games: [Game]
}

struct Game: Codable {
let gamePk: Int
let link: String
let gameType: String
let season: String
let gameDate: String
let status: Status
let teams: Team
let venue: Venue
let content: Content
}

struct Status: Codable {
let abstractGameState: String
let codedGameState: Int
let detailedState: String
let statusCode: Int
let startTimeTBD: Bool
}

struct Team: Codable {
let away: Away
let home: Home
}

struct Away: Codable {
let leagueRecord: LeagueRecord
let score: Int
let team: TeamInfo
}

struct Home: Codable {
let leagueRecord: LeagueRecord
let score: Int
let team: TeamInfo
}

struct LeagueRecord: Codable {
let wins: Int
let losses: Int
let type: String
}

struct TeamInfo: Codable {
let id: Int
let name: String
let link: String
}

struct Venue: Codable {
let name: String
let link: String
}

struct Content: Codable {
let link: String
}

这是我的 View Controller
import UIKit

class TodaysGamesTableViewController: UITableViewController {
var todaysGamesURL: URL = URL(string: "https://statsapi.web.nhl.com/api/v1/schedule")!

var gameData: [Dates] = []
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

override func viewDidLoad() {
super.viewDidLoad()
loadTodaysGames()
}

func loadTodaysGames(){
print("load Games")

view.addSubview(activityIndicator)
activityIndicator.frame = view.bounds
activityIndicator.startAnimating()

let todaysGamesDatatask = URLSession.shared.dataTask(with: todaysGamesURL, completionHandler: dataLoaded)

todaysGamesDatatask.resume()
}

func dataLoaded(data:Data?,response:URLResponse?,error:Error?){
if let detailData = data{
print("detaildata", detailData)
let decoder = JSONDecoder()
do {
let jsondata = try decoder.decode([Dates].self, from: detailData)
gameData = jsondata //Hier .instantie wil doen krijg ik ook een error

DispatchQueue.main.async{
self.tableView.reloadData()
}
}catch let error{
print(error)
}
}else{
print(error!)
}
}

最佳答案

请学会理解解码错误消息,它们非常具有描述性。

该错误表示您将解码一个数组,但实际对象是一个字典(目标结构)。

先来看看开头的JSON

{
"copyright" : "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2018. All Rights Reserved.",
"totalItems" : 2,
"totalEvents" : 0,
"totalGames" : 2,
"totalMatches" : 0,
"wait" : 10,
"dates" : [ {
"date" : "2018-05-04",

它以 { 开头这是一个字典(一个数组是 [ )但你想解码一个数组( [Dates] ),这就是错误消息所指的类型不匹配。

但这只是解决方案的一半。将线路更改为 try decoder.decode(Dates.self 后你会得到另一个错误,即没有键值 copyright .

再次查看 JSON 并将键与结构成员进行比较。其成员与 JSON 键匹配的结构是 Initial你必须得到 dates要填充的数组 gameData .
let jsondata = try decoder.decode(Initial.self, from: detailData)
gameData = jsondata.dates

关于json - debugDescription : "Expected to decode Array<Any> but found a dictionary instead.", 底层错误:无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179922/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com