首页 > 解决方案 > 如何在 Swift 中使用对象数组解析 JSON 文件?

问题描述

几个小时以来,我一直在尝试解析一个带有数组的 JSON 文件来试验 API,但我不断收到错误。我设法解析了一个只有一个 JSON 对象/字典的文件(遵循不同的教程),但我无法弄清楚一个包含对象数组的 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
}

提前感谢您的帮助!

标签: jsonswiftjsondecoder

解决方案


这里有两点值得一提

  1. 您尝试解析的响应应该来自 https://coronavirus-19-api.herokuapp.com/countries而不是来自https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats . 所以使用第一个链接而不是第二个。
        let url = "https://coronavirus-19-api.herokuapp.com/countries"
        getData(from: url)
  1. 由于有两个带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
}

推荐阅读