使用swift解码巨大的JSON数组URL

问题描述 投票:-1回答:1

[我正在尝试从这个庞大的JSON数组https://coronavirus-19-api.herokuapp.com/countries中解码数据,我曾按国家/地区或全球范围内的总统计信息进行过运气解码https://coronavirus-19-api.herokuapp.com/all通过执行以下

//
//  GlobalSwiftViewController.swift
//  Universal
//
//  Created by JOE on 3/20/20.


import UIKit

final class StatSwiftViewController: UIViewController {


   // THESE LABELS WILL RETRIEVE THE FOLLOWING DATA FROM THE URL: THE CASE , DEATH AND RECOVERED DATA
    @IBOutlet weak var CaseLable: UILabel! 

    @IBOutlet weak var DeathLable: UILabel!

    @IBOutlet weak var RecoveredLabel: UILabel!

    struct JSONTest: Decodable {
    let cases: Double
    let deaths: Float
    let recovered: Int?
}

    override func viewDidLoad() {
        super.viewDidLoad()

        let urlString = "https://coronavirus-19-api.herokuapp.com/all"
        guard let url = URL(string: urlString) else { return }

        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error != nil {
                print(error!.localizedDescription)
            }

            guard let data = data else { return }
            do {
                //Decode data
                let urlString = try JSONDecoder().decode(JSONTest.self, from: data)
                let numberFormatter = NumberFormatter()
                numberFormatter.numberStyle = .decimal

                //HERE WE ARE SHOWING TO THE USER THE DATA FROM THE URL ABOVE
                DispatchQueue.main.async {
                    self.CaseLable.text = numberFormatter.string(for: urlString.cases) 
                    self.DeathLable.text = numberFormatter.string(for: urlString.deaths)
                    self.RecoveredLabel.text = numberFormatter.string(for: urlString.recovered)

                    //self.timeLabel.text = JSONTest.deaths
                }

            } catch let jsonError {
                print(jsonError)
            }

            }.resume()
    }

}

现在,我尝试解码此URL https://coronavirus-19-api.herokuapp.com/countries中的所有数据以在一个视图控制器中显示,通过使用与上面相同的代码,将该国家/地区的单个URL https://coronavirus-19-api.herokuapp.com/countries/china用于国家/地区,我已经取得了成功更多的变量和标签但是,我无法通过为每个国家/地区添加每个URL或为所有国家/地区使用主URL来添加更多县https://coronavirus-19-api.herokuapp.com/countries因此,H ow我可以使用所有国家/地区的URL来构造所有数组列表国家?注意:我试图在上面编辑/更新我的代码以尽可能获得结果,而无需安装额外的Pod或文件...

stack-overflow
1个回答
1
投票

尝试调整模型以能够解码国家/地区数据。

您可以在操场上进行测试:

import Foundation

struct JSONTestElement: Codable {
    let country: String
    let cases, todayCases, deaths, todayDeaths: Int
    let recovered, active, critical, casesPerOneMillion: Int
}

typealias JSONTest = [JSONTestElement]

func decode() {

    let urlString = "https://coronavirus-19-api.herokuapp.com/countries"
    guard let url = URL(string: urlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if error != nil {
            print(error!.localizedDescription)
        }

        guard let data = data else { return }

        do {
            //Decode data
            let countriesData = try JSONDecoder().decode(JSONTest.self, from: data)

            let china = countriesData.filter({ $0.country.contains("China")})

            print("China data: \(china)")

        } catch let jsonError {
             print(jsonError)
        }

     }.resume()
}

decode()
© www.soinside.com 2019 - 2024. All rights reserved.