将 JSON 解析为 Swift 时消除快速类型附件方法
Eliminating quicktype accessory methods when parsng JSON to Swift
我注意到虽然 Quicktype.io 在将 JSON 解析为 SWIFt 方面做得很好,但有时它会求助于很多辅助功能和方法。对于以下内容,它生成了大约 15 个额外的方法。其中一些很熟悉,例如 NSNull,但是,有两个对我来说并不熟悉,例如 JSONAny,似乎应该有办法绕过它们。例如,JSONAny class 中有大约 12 个函数,它仅用于解析一个对我来说不那么重要的字段。
这是 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.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"}]
我不会给出该结构比主结构低一级的所有代码:
struct CountryReturnedElement: Codable {
//...various fields
let regionalBlocs: [RegionalBloc]
}
// MARK: - RegionalBloc
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [JSONAny]
}
它仅用于解码以下 JSON:
"regionalBlocs":[{"acronym":"SAARC","name":"South Asian Association for Regional Cooperation","otherAcronyms":[],"otherNames":[]}]
是否有一种简单的方法来解析上述内容,而无需借助具有 15 个函数和方法的辅助 classes。我很可能,otherAcronyms 和 otherNames 是字符串,所以我想我可以去 [String?]。但是,我想我不知道有 100% 的把握,更像是 95% 的把握。
感谢您的任何建议。
如果您确定 otherAcronyms
和 otherNames
键 return [String?]
您可以修改 RegionalBloc
结构以接受 [String?]
.
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [String?]
}
您可以简单地尝试一下,如果 JSONDecoder
没有抛出任何错误,您就可以继续 [String?]
。否则,你可以检查错误并打印到控制台来检查传入类型并设置它。
我注意到虽然 Quicktype.io 在将 JSON 解析为 SWIFt 方面做得很好,但有时它会求助于很多辅助功能和方法。对于以下内容,它生成了大约 15 个额外的方法。其中一些很熟悉,例如 NSNull,但是,有两个对我来说并不熟悉,例如 JSONAny,似乎应该有办法绕过它们。例如,JSONAny class 中有大约 12 个函数,它仅用于解析一个对我来说不那么重要的字段。
这是 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.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"}]
我不会给出该结构比主结构低一级的所有代码:
struct CountryReturnedElement: Codable {
//...various fields
let regionalBlocs: [RegionalBloc]
}
// MARK: - RegionalBloc
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [JSONAny]
}
它仅用于解码以下 JSON:
"regionalBlocs":[{"acronym":"SAARC","name":"South Asian Association for Regional Cooperation","otherAcronyms":[],"otherNames":[]}]
是否有一种简单的方法来解析上述内容,而无需借助具有 15 个函数和方法的辅助 classes。我很可能,otherAcronyms 和 otherNames 是字符串,所以我想我可以去 [String?]。但是,我想我不知道有 100% 的把握,更像是 95% 的把握。
感谢您的任何建议。
如果您确定 otherAcronyms
和 otherNames
键 return [String?]
您可以修改 RegionalBloc
结构以接受 [String?]
.
struct RegionalBloc: Codable {
let acronym, name: String
let otherAcronyms, otherNames: [String?]
}
您可以简单地尝试一下,如果 JSONDecoder
没有抛出任何错误,您就可以继续 [String?]
。否则,你可以检查错误并打印到控制台来检查传入类型并设置它。