问题
I've noticed that while Quicktype.io does a very good job of parsing JSON into SWIFt, occasionally it resorts to a lot of auxiliary functions and methods. For the following, it generated about 15 additional methods. Some of these are familiar such as NSNull, however, two are unfamiliar to me such as JSONAny and it seems like there ought to be a way around them. The JSONAny class for example, has about 12 functions in it and it is used to parse just one field that is not that important to me.
Here is what the JSON looks like:
[{"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.0,65.0],"demonym":"Afghan","area":652230.0,"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"}]
I wont' give all the code that that struct is one level down from the main struct:
struct CountryReturnedElement: Codable {
//...various fields
let regionalBlocs: [RegionalBloc]
}
// MARK: - RegionalBloc
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [JSONAny]
}
Which is designed to decode merely the following JSON:
"regionalBlocs":[{"acronym":"SAARC","name":"South Asian Association for Regional Cooperation","otherAcronyms":[],"otherNames":[]}]
Is there a simple way to parse the above without resorting to auxiliary classes with literally fifteen functions and methods. I all likelihood, otherAcronyms and otherNames are strings so I guess I could go [String?]. However, I guess I don't know that with 100% certainty, more like 95% certainty.
Thanks for any suggestions.
回答1:
If you're certain the otherAcronyms
and otherNames
keys return [String?]
you can modify the RegionalBloc
struct to accept [String?]
.
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [String?]
}
You can simply try it out and if the JSONDecoder
doesn't throw any error you're good and can continue with [String?]
. Otherwise, you can check the error and print it out onto the console to check the incoming type and set it.
来源:https://stackoverflow.com/questions/63138535/eliminating-quicktype-accessory-methods-when-parsng-json-to-swift