我在解码下面的json时遇到了一个问题,因为我有一个不同数据类型的字典数组,Any不支持codable?
我可以在模型中做些什么改变来使它工作?谢谢。
这里是json
"alerts":[
{
"id":"https://api.weather.gov/alerts/urn:oid:2.49.0.1.840.0.eae34c2b3a065ca71d8746f3c67123c93a12257d.002.1",
"areaDescription":"Ashland, OH; Knox, OH; Richland, OH",
"dateSent":1680365400,
"dateEffective":1680365400,
"dateOnset":1680365400,
"dateExpires":1680366600,
"dateEnds":1680366600,
"severity":"Severe",
"certainty":"Observed",
"urgency":"Immediate",
"senderName":"NWS Cleveland OH",
"description":"At 1208 PM EDT, severe thunderstorms were located along a line extending from near Nashville to 7 miles east of Utica, moving east at 60 mph.\n\nHAZARD...60 mph wind gusts.\n\nSOURCE...Radar indicated.\n\nIMPACT...Expect damage to trees and power lines.\n\nLocations impacted include... Mansfield, Mount Vernon, Lexington, Loudonville, Fredericktown, Gambier, Utica, Bellville, Centerburg, Danville, Waterford, Butler, Perrysville, Lucas, Martinsburg, Gann, North Liberty and Bladensburg.",
"instruction":"For your protection move to an interior room on the lowest floor of a building.",
"response":"Shelter",
"event":"Severe Thunderstorm Warning",
"headline":"Severe Thunderstorm Warning issued April 1 at 12:10PM EDT until April 1 at 12:30PM EDT by NWS Cleveland OH",
"nwsHeadline":[
"A SEVERE THUNDERSTORM WARNING REMAINS IN EFFECT UNTIL 1230 PM EDT FOR SOUTHERN RICHLAND...SOUTHEASTERN ASHLAND AND KNOX COUNTIES"
],
"polygon":[
[
40.24,
-82.48
],
[
40.27,
-82.48
],
[
40.28,
-82.74000000000001
],
[
40.35,
-82.75000000000001
],
[
40.35,
-82.65000000000002
],
[
40.550000000000004,
-82.64000000000001
],
[
40.550000000000004,
-82.62000000000002
],
[
40.67,
-82.65000000000002
],
[
40.67,
-82.62000000000002
],
[
40.71,
-82.62000000000002
],
[
40.72,
-82.73000000000002
],
[
40.729999899999996,
-82.73000000000002
],
[
40.709999999999994,
-82.13000000000002
],
[
40.669999999999995,
-82.13000000000002
],
[
40.669999999999995,
-82.22000000000003
],
[
40.56999999999999,
-82.22000000000003
],
[
40.56999999999999,
-82.18000000000002
],
[
40.239999999999995,
-82.20000000000002
],
[
40.24,
-82.48
]
],
"multiPolygon":[
[
[
40.24,
-82.48
],
[
40.27,
-82.48
],
[
40.28,
-82.74000000000001
],
[
40.35,
-82.75000000000001
],
[
40.35,
-82.65000000000002
],
[
40.550000000000004,
-82.64000000000001
],
[
40.550000000000004,
-82.62000000000002
],
[
40.67,
-82.65000000000002
],
[
40.67,
-82.62000000000002
],
[
40.71,
-82.62000000000002
],
[
40.72,
-82.73000000000002
],
[
40.729999899999996,
-82.73000000000002
],
[
40.709999999999994,
-82.13000000000002
],
[
40.669999999999995,
-82.13000000000002
],
[
40.669999999999995,
-82.22000000000003
],
[
40.56999999999999,
-82.22000000000003
],
[
40.56999999999999,
-82.18000000000002
],
[
40.239999999999995,
-82.20000000000002
],
[
40.24,
-82.48
]
]
],
"affectedZoneIDs":[
{
"type":"county",
"id":"OHC005"
},
{
"type":"county",
"id":"OHC083"
},
{
"type":"county",
"id":"OHC139"
}
],
"messageType":"update"
},
{,
以下是我现在拥有的:
func getWeatherAlerts() -> AnyPublisher<Alerts, Error> {
guard let url = URL(string: "https://api.alltheapps.org/weather/v3/allActiveAlerts?apiKey=da230328") else {
return Fail(error: CustomError.invalidUrl).eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.catch { error in
return Fail(error: error).eraseToAnyPublisher()
}.map( {$0.data})
.decode(type: Alerts.self, decoder: JSONDecoder())
.eraseToAnyPublisher()
}
以及Map
struct Alerts: Decodable {
let alerts: [[String:String]]
}
enum CodingKeys: String, CodingKey {
case alertTitle = "event"
case startDateAlert = "dateOnset"
case endDateAlert = "dateEnds"
case sourceOfAlert = "senderName"
}
struct WeatherAlertsModel:Decodable {
var alertTitle:String
var startDateAlert:Date
var endDateAlert:Date
var sourceOfAlert:String
var severity:String
var certainty:String
var urgency:String
var description:String
var affectedZoneIDs:[String]
}
它给予了我一个解析错误,因为我有[String:String],而json也有[String:Int]或[String:Date]
1条答案
按热度按时间zyfwsgd61#
可以使用如下结构: