大佬帮忙将下面的JSON转成Model"result": {
swift吧
全部回复
仅看楼主
level 1
大佬帮忙将下面的JSON转成Model
"result": { "city": "芜湖", "weather": [ { "date": "2020-04-20", "temperature": "13/21℃", "weather": "小雨", "direct": "东北风" }, { "date": "2020-04-21", "temperature": "11/18℃", "weather": "阴", "direct": "西南风" }, { "date": "2020-04-22", "temperature": "9/18℃", "weather": "多云", "direct": "东北风" }, { "date": "2020-04-23", "temperature": "6/15℃", "weather": "多云", "direct": "西风转西南风" }, { "date": "2020-04-24", "temperature": "10/19℃", "weather": "多云转晴", "direct": "西南风" } ] }}
2020年06月06日 07点06分 1
level 1
[泪]
2020年06月06日 07点06分 2
level 6

2020年06月07日 12点06分 3
level 6
import Foundation
struct WeatherInfoModelBaseModel: Codable {
enum CodingKeys: String, CodingKey {
case city = "city"
case weather = "weather"
}
var city: String?
var weather: [WeatherInfoModelWeather]?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
city = try container.decodeIfPresent(String.self, forKey: .city)
weather = try container.decodeIfPresent([WeatherInfoModelWeather].self, forKey: .weather)
}
}
struct WeatherInfoModelWeather: Codable {
enum CodingKeys: String, CodingKey {
case temperature = "temperature"
case direct = "direct"
case weather = "weather"
case date = "date"
}
var temperature: String?
var direct: String?
var weather: String?
var date: String?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
temperature = try container.decodeIfPresent(String.self, forKey: .temperature)
direct = try container.decodeIfPresent(String.self, forKey: .direct)
weather = try container.decodeIfPresent(String.self, forKey: .weather)
date = try container.decodeIfPresent(String.self, forKey: .date)
}
}
2020年06月08日 16点06分 4
1