Correctly Parsing JSON in Swift 3

前端 未结 9 969
甜味超标
甜味超标 2020-11-21 07:00

I\'m trying to fetch a JSON response and store the results in a variable. I\'ve had versions of this code work in previous releases of Swift, until the GM version of Xcode 8

9条回答
  •  终归单人心
    2020-11-21 07:45

    I built quicktype exactly for this purpose. Just paste your sample JSON and quicktype generates this type hierarchy for your API data:

    struct Forecast {
        let hourly: Hourly
        let daily: Daily
        let currently: Currently
        let flags: Flags
        let longitude: Double
        let latitude: Double
        let offset: Int
        let timezone: String
    }
    
    struct Hourly {
        let icon: String
        let data: [Currently]
        let summary: String
    }
    
    struct Daily {
        let icon: String
        let data: [Datum]
        let summary: String
    }
    
    struct Datum {
        let precipIntensityMax: Double
        let apparentTemperatureMinTime: Int
        let apparentTemperatureLowTime: Int
        let apparentTemperatureHighTime: Int
        let apparentTemperatureHigh: Double
        let apparentTemperatureLow: Double
        let apparentTemperatureMaxTime: Int
        let apparentTemperatureMax: Double
        let apparentTemperatureMin: Double
        let icon: String
        let dewPoint: Double
        let cloudCover: Double
        let humidity: Double
        let ozone: Double
        let moonPhase: Double
        let precipIntensity: Double
        let temperatureHigh: Double
        let pressure: Double
        let precipProbability: Double
        let precipIntensityMaxTime: Int
        let precipType: String?
        let sunriseTime: Int
        let summary: String
        let sunsetTime: Int
        let temperatureMax: Double
        let time: Int
        let temperatureLow: Double
        let temperatureHighTime: Int
        let temperatureLowTime: Int
        let temperatureMin: Double
        let temperatureMaxTime: Int
        let temperatureMinTime: Int
        let uvIndexTime: Int
        let windGust: Double
        let uvIndex: Int
        let windBearing: Int
        let windGustTime: Int
        let windSpeed: Double
    }
    
    struct Currently {
        let precipProbability: Double
        let humidity: Double
        let cloudCover: Double
        let apparentTemperature: Double
        let dewPoint: Double
        let ozone: Double
        let icon: String
        let precipIntensity: Double
        let temperature: Double
        let pressure: Double
        let precipType: String?
        let summary: String
        let uvIndex: Int
        let windGust: Double
        let time: Int
        let windBearing: Int
        let windSpeed: Double
    }
    
    struct Flags {
        let sources: [String]
        let isdStations: [String]
        let units: String
    }
    

    It also generates dependency-free marshaling code to coax the return value of JSONSerialization.jsonObject into a Forecast, including a convenience constructor that takes a JSON string so you can quickly parse a strongly typed Forecast value and access its fields:

    let forecast = Forecast.from(json: jsonString)!
    print(forecast.daily.data[0].windGustTime)
    

    You can install quicktype from npm with npm i -g quicktype or use the web UI to get the complete generated code to paste into your playground.

提交回复
热议问题