如何将JSON数据转换为模型

时间:2019-11-09 07:57:34

标签: ios json swift swift5

我有restcountries.eu ulr,需要在模型formate中对其进行隐蔽,但出现错误

错误:由于数据格式不正确,因此无法读取。

这是我的JSON。

{
  "name": "Afghanistan",
  "topLevelDomain": [
    ".af"
  ],
  "alpha2Code": "AF",
  "alpha3Code": "AFG",
  "callingCodes": [
    "93"
  ],
  "capital": "Kabul",
  "altSpellings": [
    "AF",
    "Afġānistān"
  ],
  "region": "Asia",
  "subregion": "Southern Asia",
  "population": 27657145,
  "latlng": [
    33,
    65
  ],
  "demonym": "Afghan",
  "area": 652230,
  "gini": 27.8,
  "timezones": [
    "UTC+04:30"
  ],
  "borders": [
    "IRN",
    "PAK",
    "TKM",
    "UZB",
    "TJK",
    "CHN"
  ],
  "nativeName": "افغانستان",
  "numericCode": "004",
  "currencies": [
    {
      "code": "AFN",
      "name": "Afghan afghani",
      "symbol": "؋"
    }
  ],
  "languages": [
    {
      "iso639_1": "ps",
      "iso639_2": "pus",
      "name": "Pashto",
      "nativeName": "پښتو"
    },
    {
      "iso639_1": "uz",
      "iso639_2": "uzb",
      "name": "Uzbek",
      "nativeName": "Oʻzbek"
    },
    {
      "iso639_1": "tk",
      "iso639_2": "tuk",
      "name": "Turkmen",
      "nativeName": "Türkmen"
    }
  ],
  "translations": {
    "de": "Afghanistan",
    "es": "Afganistán",
    "fr": "Afghanistan",
    "ja": "アフガニスタン",
    "it": "Afghanistan",
    "br": "Afeganistão",
    "pt": "Afeganistão",
    "nl": "Afghanistan",
    "hr": "Afganistan",
    "fa": "افغانستان"
  },
  "flag": "https://restcountries.eu/data/afg.svg",
  "regionalBlocs": [
    {
      "acronym": "SAARC",
      "name": "South Asian Association for Regional Cooperation",
      "otherAcronyms": [],
      "otherNames": []
    }
  ],
  "cioc": "AFG"
}

我为上述JSON创建了模型

创建模型

class Country: Codable {
    var name: String?
    var topLevelDomain: [String]?
    var alpha2Code: String?
    var alpha3Code: String?
    var callingCodes: [String]?
    var capital: String?
    var altSpellings: [String]?
    var region: String?
    var subregion: String?
    var population: CLong?
    var latlng: [Double]?
    var demonym: String?
    var area: CLong?
    var gini: Double?
    var timezones: [String]?
    var borders: [String]?
    var nativeName: String?
    var numericCode: String?
    var currencies: [Currencie]?
    var languages: [Language]?
    var translations: Translation?
    var flag: String?
    var regionalBlocs: [RegionalBloc]?
    var cioc: String?
}

第二个模型作为子模型

class Currencie: Codable {
    var code: String?
    var name: String?
    var symbol: String?
}

class Language: Codable {
    var iso639_1: String?
    var iso639_2: String?
    var name: String?
    var nativeName: String?
}

第三个模型作为子模型

class Translation: Codable {
    var de: String?
    var es: String?
    var fr: String?
    var ja: String?
    var it: String?
    var br: String?
    var pt: String?
    var nl: String?
    var hr: String?
    var fa: String?
}

第4个模型作为子模型

class RegionalBloc: Codable {
    var acronym: String?
    var name: String?
    var otherAcronyms: [String?]?
    var otherNames: [String?]?
}

当我将其从数据解码为以下模型时

decoder.decode([Country].self, from: data)

出现错误:错误:由于格式不正确,无法读取数据。

2 个答案:

答案 0 :(得分:0)

根据您的JSON,仅存在Country的一个对象

当json具有国家的多个对象时,它将起作用。

do {
    let decoder = JSONDecoder()
    let country = try decoder.decode([Country].self, from: jsonData)
    print(country)
}
catch {
    print(error)
}

根据当前JSON:

Solution:代替[Country].self来使用Country.self

do {
    let decoder = JSONDecoder()
    let country = try decoder.decode(Country.self, from: jsonData)
    print(country)
}
catch {
    print(error)
}

答案 1 :(得分:0)

使用以下模型解码JSON

https://app.quicktype.io/

do {
    let decoder = JSONDecoder()
    let country = try decoder.decode(Country.self, from: jsonData)
    print(country)
}
catch {
    print(error)
}



// MARK: - Country
struct Country: Codable {
    let name: String
    let topLevelDomain: [String]
    let alpha2Code, alpha3Code: String
    let callingCodes: [String]
    let capital: String
    let altSpellings: [String]
    let region, subregion: String
    let population: Int
    let latlng: [Int]
    let demonym: String
    let area: Int
    let gini: Double
    let timezones, borders: [String]
    let nativeName, numericCode: String
    let currencies: [Currency]
    let languages: [Language]
    let translations: Translations
    let flag: String
    let regionalBlocs: [RegionalBloc]
    let cioc: String
}

// MARK: - Currency
struct Currency: Codable {
    let code, name, symbol: String
}

// MARK: - Language
struct Language: Codable {
    let iso6391, iso6392, name, nativeName: String

    enum CodingKeys: String, CodingKey {
        case iso6391 = "iso639_1"
        case iso6392 = "iso639_2"
        case name, nativeName
    }
}

// MARK: - RegionalBloc
struct RegionalBloc: Codable {
    let acronym, name: String
    let otherAcronyms, otherNames: [JSONAny]
}

// MARK: - Translations
struct Translations: Codable {
    let de, es, fr, ja: String
    let it, br, pt, nl: String
    let hr, fa: String
}