我正在尝试创建一种更简洁的可编码结构,在其中我可以通过键入day.description
而不是day.weather.description
来访问“说明”
描述值嵌套在“天气”数组中,该数组仅包含一个对象。我想从索引0中提取描述,并将其分配给我的结构中的描述。
这是我正在使用的JSON:
{
"dt": 1558321200,
"main": {
"temp": 11.88,
"temp_min": 11.88,
"temp_max": 11.88,
"pressure": 1013.3,
"sea_level": 1013.3,
"grnd_level": 1003.36,
"humidity": 77,
"temp_kf": 0
},
"weather": [{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}],
"clouds": {
"all": 0
},
"wind": {
"speed": 5.58,
"deg": 275.601
},
"sys": {
"pod": "n"
},
"dt_txt": "2019-05-20 03:00:00"
}
以及到目前为止的代码:
struct Weather: Codable {
let days: [Day]
enum CodingKeys: String, CodingKey {
case days = "list"
}
}
struct Day: Codable {
let date: String
let main: Main
let wind: Wind
let description: String
enum CodingKeys: String, CodingKey {
case date = "dt_txt"
case main
case wind
case weather
case description
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(String.self, forKey: .date)
main = try container.decode(Main.self, forKey: .main)
wind = try container.decode(Wind.self, forKey: .wind)
let weather = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .weather)
description = try weather.decode(String.self, forKey: .description)
}
}
答案 0 :(得分:0)
如您所知,最简单的方法只是引用所需值的计算属性。但是,出于完整性考虑,我们不妨讨论如何执行您实际要求做的事情。让我们用JSON的简化版本进行说明:
{
"dt": 1558321200,
"weather": [{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}]
}
所以问题是,如何将其解析为具有description
属性的结构结果,以便我们从{{1}中的第一项中取出"description"
键数组?这是一种方法:
"weather"
基本上,这里的想法是struct Result : Decodable {
let description : String
enum Keys : CodingKey {
case weather
}
struct Weather : Decodable {
let description : String
}
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: Keys.self)
var arr = try! con.nestedUnkeyedContainer(forKey: .weather) // weather array
let oneWeather = try! arr.decode(Weather.self) // decode first element
self.description = oneWeather.description
}
}
给我们我们的数组,随后对该数组上的nestedUnkeyedContainer
的调用会自动依次处理每个元素。我们只有一个元素,因此我们只需要一个decode
调用。我们如何处理结果字符串取决于我们自己,因此现在我们可以将其放入顶级decode
属性中。
但这是另一种方法。我们甚至根本不需要二级Weather结构。我们可以直接进入description
数组并获取第一个字典元素并访问其"weather"
键,而无需再说内部字典中的内容,就像这样:
"description"
您的问题不是很完整(您没有显示您的 real JSON,只是摘录),所以我无法提供更精确的建议,但是我敢肯定您可以看到如何使这项技术适应您的需求。