尝试解析JSON时无法将类型“ MRData”的值分配给类型“ [F1Data]”

时间:2019-03-20 20:15:41

标签: json swift4 codable decoder

我已经为此努力了一段时间。我正在尝试将JSON Api解析为UITableview。网址为Formula One API。我使用的是Codable,而不是第三方的广告连播。认为这可能会减少代码量。虽然,由于API并非那么简单,所以很难提取出我想要的东西。基本上,我想列出给定年份的驾驶员当前身分。在url和代码中,我选择了1999作为示例。我一直在研究Stackoverflow,但是每种解决方案都是特定于特定问题的,并且似乎与我的问题无关。下面是我的代码。

struct MRData: Codable {
let xmlns: String?
let series: String?
let url: String?
let limit, offset, total: String?
let standingsTable: StandingsTable

enum CodingKeys: String, CodingKey {
    case xmlns, series, url, limit, offset, total
    case standingsTable = "StandingsTable"
 }
}

struct StandingsTable: Codable {
let season: String?
let standingsLists: [StandingsList]

enum CodingKeys: String, CodingKey {
    case season
    case standingsLists = "StandingsLists"
 }
}

struct StandingsList: Codable {
let season, round: String?
let driverStandings: [DriverStanding]

enum CodingKeys: String, CodingKey {
    case season, round
    case driverStandings = "DriverStandings"
  }
 }

struct DriverStanding: Codable {
let position, positionText, points, wins: String?
let driver: Driver
let constructors: [Constructor]

enum CodingKeys: String, CodingKey {
    case position, positionText, points, wins
    case driver = "Driver"
    case constructors = "Constructors"
 }
 }

struct Constructor: Codable {
let constructorId: String?
let url: String?
let name: String?
let nationality: String?
}

struct Driver: Codable {
let driverId: String?
let url: String?
let givenName, familyName, dateOfBirth, nationality: String?
}

class f1TableViewController: UITableViewController {

var champions: [F1Data] = []

override func viewDidLoad() {
    super.viewDidLoad()
    //        let jsonUrlString = "https://api.letsbuildthatapp.com/jsondecodable/website_description"
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.title = "Champion Drivers"
    fetchJSON()
}
private func fetchJSON(){
    let jsonUrlString = "https://ergast.com/api/f1/1999/driverstandings.json"

    guard let url = URL(string: jsonUrlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        DispatchQueue.main.async {
            if let err = err {
                print("Failed to get data from url:", err)
                return
            }

            guard let data = data else { return }
            do {
                let decoder = JSONDecoder()
                // Swift 4.1
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                self.champions = try decoder.decode(MRData.self, from: data)
                self.tableView.reloadData()
                //let season = f1Data.mrData.standingsTable.season

                //                let firstDriver = f1Data.mrData.standingsTable.standingsLists[0].driverStandings
                //                for driver in firstDriver {
                //
                //                    print("\(driver.driver.givenName) \(driver.driver.familyName)")
                //                }
                //print(season)
            } catch {
                print(error)
            }
        }
        }.resume()
}
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return champions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")

    let champion = champions[indexPath.row]
    let driverName = champion.mrData.standingsTable.standingsLists[0].driverStandings
    for driver in driverName {
        cell.textLabel?.text = driver.driver.familyName
    }
    //cell.textLabel?.text =
    //cell.detailTextLabel?.text = String(course.numberOfLessons)
    return cell
   }

}

现在我意识到错误在于do catch块中。

do {
                let decoder = JSONDecoder()
                // Swift 4.1
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                self.champions = try decoder.decode(MRData.self, from: data)
                self.tableView.reloadData()

,并且数组F1Data不能是MRData的字典。因此,如果将其更改为以下self.champions = try decoder.decode([F1Data].self, from: data),则会收到另一个错误 debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

正如Vadian正确地说的那样,您需要在对象的根部进行解码。我短暂地观看了此video,一分钱掉了!将解码器分配给变量,然后添加以对从根对象开始的完整结构进行解码。

guard let data = data else { return }
            do {
                let decoder = JSONDecoder()
                // Swift 4.1
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                let firstDriver = try decoder.decode(F1Data.self, from: data)
                self.champions = firstDriver.mrData.standingsTable.standingsLists[0].driverStandings
                self.tableView.reloadData()