Вызов API и JSON декодирование - PullRequest
1 голос
/ 26 мая 2020

Обнаружена ошибка при попытке выполнить запрос API с последующим JSON декодированием в поле данных.

Цель состоит в том, чтобы запросить API и отобразить данные в Словаре в приложении для упрощения запроса данных по странам.

Вот формат API, ключ API не требуется:

{
  "Global": {
    "NewConfirmed": 98206,
    "TotalConfirmed": 5492996,
    "NewDeaths": 3030,
    "TotalDeaths": 351576,
    "NewRecovered": 56379,
    "TotalRecovered": 2167913
  },
  "Countries": [
    {
      "Country": "Afghanistan",
      "CountryCode": "AF",
      "Slug": "afghanistan",
      "NewConfirmed": 584,
      "TotalConfirmed": 10582,
      "NewDeaths": 2,
      "TotalDeaths": 218,
      "NewRecovered": 35,
      "TotalRecovered": 1075,
      "Date": "2020-05-26T03:04:40Z"
    },
    {
      "Country": "Albania",
      "CountryCode": "AL",
      "Slug": "albania",
      "NewConfirmed": 9,
      "TotalConfirmed": 998,
      "NewDeaths": 1,..........

А вот структуры полей данных, я думаю, в чем моя проблема, но я новичок и не уверен в правильной структуре / форматирование:

struct GlobalSum: Decodable {
    let globalNC: Int
    let globalTC: Int
    let globalND: Int
    let globalTD: Int
    let globalNR: Int
    let globalTR: Int
    let sumByCntry: [CountrySum]

    enum CodingKeys: String, CodingKey {
        case globalNC = "NewConfirmed"
        case globalTC = "TotalConfirmed"
        case globalND = "NewDeaths"
        case globalTD = "TotalDeaths"
        case globalNR = "NewRecovered"
        case globalTR = "TotalRecovered"
        case sumByCntry = "Countries"
    }
}

struct CountrySum: Decodable {
    let Country: String
    let CountryCode: String
    let Slug: String
    let NewConfirmed: Int
    let TotalConfirmed: Int
    let NewDeaths: Int
    let TotalDeaths: Int
    let NewRecovered: Int
    let TotalRecovered: Int
    let Date: String

}

Вот вызов API и JSON декодирование:

func getSummary() {
        let callString = "https://api.covid19api.com/summary"
        let urlCall = URL(string: callString)
        guard urlCall != nil else {
            print("Error reaching API")
            return
        }
        let session = URLSession.shared
        let dataTask = session.dataTask(with: urlCall!) { (data, response, error) in
            if error == nil && data != nil {
                let decoder = JSONDecoder()
                do {
                    let sumPull = try decoder.decode(GlobalSum.self, from: data!)
                    print(sumPull.globalNC)
                }
                catch {
                    print(error)               }
            }
        }
        dataTask.resume()
    }

И вот ошибка:

dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

Любые предложения были бы очень признательны. Приносим извинения за очевидные грубые ошибки. Только начинаем заниматься самообразованием на Swift.

Ответы [ 2 ]

0 голосов
/ 26 мая 2020

Обновите вашу модель вот так и попробуйте разобрать ее. Вот мой код.

Вот правильная структура:

import Foundation

struct Summary: Codable {
    let global: Global
    let countries: [Country]
    let date: String

    enum CodingKeys: String, CodingKey {
        case global    = "Global"
        case countries = "Countries"
        case date      = "Date"
    }
}

struct Country: Codable {
    let country, countryCode, slug: String
    let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
    let newRecovered, totalRecovered: Int
    let date: String

    enum CodingKeys: String, CodingKey {
        case country        = "Country"
        case countryCode    = "CountryCode"
        case slug           = "Slug"
        case newConfirmed   = "NewConfirmed"
        case totalConfirmed = "TotalConfirmed"
        case newDeaths      = "NewDeaths"
        case totalDeaths    = "TotalDeaths"
        case newRecovered   = "NewRecovered"
        case totalRecovered = "TotalRecovered"
        case date           = "Date"
    }
}

struct Global: Codable {
    let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
    let newRecovered, totalRecovered: Int

    enum CodingKeys: String, CodingKey {
        case newConfirmed   = "NewConfirmed"
        case totalConfirmed = "TotalConfirmed"
        case newDeaths      = "NewDeaths"
        case totalDeaths    = "TotalDeaths"
        case newRecovered   = "NewRecovered"
        case totalRecovered = "TotalRecovered"
    }
}

Вот вызов API и JSON декодирование:

func getSummary() {
    let callString = "https://api.covid19api.com/summary"
    let urlCall = URL(string: callString)
    guard urlCall != nil else {
        print("Error reaching API")
        return
    }
    let session = URLSession.shared
    let dataTask = session.dataTask(with: urlCall!) { (data, response, error) in
        if error == nil && data != nil {
            let decoder = JSONDecoder()
            do {
                let sumPull = try decoder.decode(Summary.self, from: data!)
                print(sumPull.global)
            }
            catch {
                print(error)
            }
        }
    }
    dataTask.resume()
}
0 голосов
/ 26 мая 2020

То, что я получаю от json .. ваш модальный должен быть таким

 struct CoronaCases {
        let global : GlobalSum
        let countries: CountrySum
        let date: String

        enum CodingKeys: String, CodingKey {
            case global = "Global"
            case countries = "Countries"
            case date = "Date"

        }
    }

    struct GlobalSum: Decodable {
        let globalNC: Int
        let globalTC: Int
        let globalND: Int
        let globalTD: Int
        let globalNR: Int
        let globalTR: Int


        enum CodingKeys: String, CodingKey {
            case globalNC = "NewConfirmed"
            case globalTC = "TotalConfirmed"
            case globalND = "NewDeaths"
            case globalTD = "TotalDeaths"
            case globalNR = "NewRecovered"
            case globalTR = "TotalRecovered"

        }
    }
    struct CountrySum: Decodable {
        let Country: String
        let CountryCode: String
        let Slug: String
        let NewConfirmed: Int
        let TotalConfirmed: Int
        let NewDeaths: Int
        let TotalDeaths: Int
        let NewRecovered: Int
        let TotalRecovered: Int
        let Date: String

    }

А затем

let sumPull = try decoder.decode(CoronaCases.self, from: data!)

и вот что сгенерировано app.quicktype.io

import Foundation

// MARK: - Welcome
struct Welcome: Codable {
    let global: Global
    let countries: [Country]
    let date: Date

    enum CodingKeys: String, CodingKey {
        case global = "Global"
        case countries = "Countries"
        case date = "Date"
    }
}

// MARK: - Country
struct Country: Codable {
    let country, countryCode, slug: String
    let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
    let newRecovered, totalRecovered: Int
    let date: Date

    enum CodingKeys: String, CodingKey {
        case country = "Country"
        case countryCode = "CountryCode"
        case slug = "Slug"
        case newConfirmed = "NewConfirmed"
        case totalConfirmed = "TotalConfirmed"
        case newDeaths = "NewDeaths"
        case totalDeaths = "TotalDeaths"
        case newRecovered = "NewRecovered"
        case totalRecovered = "TotalRecovered"
        case date = "Date"
    }
}

// MARK: - Global
struct Global: Codable {
    let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
    let newRecovered, totalRecovered: Int

    enum CodingKeys: String, CodingKey {
        case newConfirmed = "NewConfirmed"
        case totalConfirmed = "TotalConfirmed"
        case newDeaths = "NewDeaths"
        case totalDeaths = "TotalDeaths"
        case newRecovered = "NewRecovered"
        case totalRecovered = "TotalRecovered"
    }
}

enter image description here

...