如何使用Swift 4和codable从复杂的json结构访问项目以获取天气

我正在使用带有可编码的Swift 4来按城市检索天气。
我需要您的帮助。非常感谢您的帮助。
1.我为Codable创建了Struct(模型)。我把它命名为weatherCity
2.问题:如何根据以下代码访问以下项目?
    来自天气结果的谦卑,tempC,tempF和weatherIconURL,如下所示

let task = URLSession.shared.dataTask(with: serviceUrl) { data,response,error in

            guard error == nil else {
               print ("error: \(error!)")
                return
            }

            guard let data = data else {
                 print ("error: \(error!)")
                return
            }              

            guard let cityWeather = try? JSONDecoder().decode(weatherCity.self,from: data) else {
               print ("error: \(error!)")
                return
            }

            let temp = cityWeather.data?.currentCondition

----天气结果:

▿ weatherCity
  ▿ data : DataClass
    ▿ request : 1 element
      ▿ 0 : Request
        - type : "City"
        - query : "London,United Kingdom"
    ▿ currentCondition : 1 element
      ▿ 0 : CurrentCondition
        - observationTime : "02:59 AM"
        - tempC : "11"
        - tempF : "52"
        - weatherCode : "296"
        ▿ weatherIconURL : 1 element
          ▿ 0 : Weather
            - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png"
        ▿ weatherDesc : 1 element
          ▿ 0 : Weather
            - value : "Light Rain"
        - windspeedMiles : "4"
        - windspeedKmph : "7"
        - winddirDegree : "120"
        - winddir16Point : "ESE"
        - precipMM : "1.4"
        - precipInches : "0.1"
        - humidity : "94"
        - visibility : "10"
        - visibilityMiles : "6"
        - pressure : "985"
        - pressureInches : "30"
        - cloudcover : "0"
        - feelsLikeC : "10"
        - feelsLikeF : "51"
        - uvIndex : 0
    ▿ weather : 1 element
      ▿ 0 : WeatherElement
        - date : "2019-11-27"
        ▿ astronomy : 1 element
          ▿ 0 : Astronomy
            - sunrise : "07:38 AM"
            - sunset : "03:58 PM"
            - moonrise : "08:20 AM"
            - moonset : "04:49 PM"
            - moonPhase : "New Moon"
            - moonIllumination : "0"
        - maxtempC : "11"
        - maxtempF : "52"
        - mintempC : "9"
        - mintempF : "49"
        - avgtempC : "10"
        - avgtempF : "51"
        - totalsnowCM : "0.0"
        - sunHour : "3.1"
        - uvIndex : "3"
        ▿ hourly : 8 elements
          ▿ 0 : Hourly
            - time : "0"
            - tempC : "11"
            - tempF : "51"
            - windspeedMiles : "11"
            - windspeedKmph : "18"
            - winddirDegree : "174"
            - winddir16Point : "S"
            - weatherCode : "353"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Light rain shower"
            - precipMM : "1.4"
            - precipInches : "0.1"
            - humidity : "77"
            - visibility : "10"
            - visibilityMiles : "6"
            - pressure : "985"
            - pressureInches : "30"
            - cloudcover : "100"
            - heatIndexC : "11"
            - heatIndexF : "51"
            - dewPointC : "7"
            - dewPointF : "44"
            - windChillC : "8"
            - windChillF : "47"
            - windGustMiles : "17"
            - windGustKmph : "27"
            - feelsLikeC : "8"
            - feelsLikeF : "47"
            - chanceofrain : "68"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "80"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "0"
          ▿ 1 : Hourly
            - time : "300"
            - tempC : "11"
            - tempF : "51"
            - windspeedMiles : "16"
            - windspeedKmph : "26"
            - winddirDegree : "167"
            - winddir16Point : "SSE"
            - weatherCode : "296"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Light rain"
            - precipMM : "0.8"
            - precipInches : "0.0"
            - humidity : "82"
            - visibility : "9"
            - visibilityMiles : "5"
            - pressure : "980"
            - pressureInches : "29"
            - cloudcover : "100"
            - heatIndexC : "11"
            - heatIndexF : "51"
            - dewPointC : "8"
            - dewPointF : "46"
            - windChillC : "8"
            - windChillF : "46"
            - windGustMiles : "23"
            - windGustKmph : "37"
            - feelsLikeC : "8"
            - feelsLikeF : "46"
            - chanceofrain : "88"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "94"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "0"
          ▿ 2 : Hourly
            - time : "600"
            - tempC : "11"
            - tempF : "52"
            - windspeedMiles : "19"
            - windspeedKmph : "31"
            - winddirDegree : "170"
            - winddir16Point : "S"
            - weatherCode : "353"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Light rain shower"
            - precipMM : "1.6"
            - precipInches : "0.1"
            - humidity : "76"
            - visibility : "10"
            - visibilityMiles : "6"
            - pressure : "977"
            - pressureInches : "29"
            - cloudcover : "100"
            - heatIndexC : "11"
            - heatIndexF : "52"
            - dewPointC : "7"
            - dewPointF : "45"
            - windChillC : "8"
            - windChillF : "46"
            - windGustMiles : "28"
            - windGustKmph : "44"
            - feelsLikeC : "8"
            - feelsLikeF : "46"
            - chanceofrain : "75"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "89"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "0"
          ▿ 3 : Hourly
            - time : "900"
            - tempC : "11"
            - tempF : "51"
            - windspeedMiles : "25"
            - windspeedKmph : "40"
            - winddirDegree : "208"
            - winddir16Point : "SSW"
            - weatherCode : "302"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0018_cloudy_with_heavy_rain.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Moderate rain"
            - precipMM : "3.0"
            - precipInches : "0.1"
            - humidity : "78"
            - visibility : "7"
            - visibilityMiles : "4"
            - pressure : "977"
            - pressureInches : "29"
            - cloudcover : "100"
            - heatIndexC : "11"
            - heatIndexF : "51"
            - dewPointC : "7"
            - dewPointF : "44"
            - windChillC : "7"
            - windChillF : "44"
            - windGustMiles : "34"
            - windGustKmph : "55"
            - feelsLikeC : "7"
            - feelsLikeF : "44"
            - chanceofrain : "89"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "89"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "3"
          ▿ 4 : Hourly
            - time : "1200"
            - tempC : "10"
            - tempF : "51"
            - windspeedMiles : "19"
            - windspeedKmph : "31"
            - winddirDegree : "242"
            - winddir16Point : "WSW"
            - weatherCode : "302"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0018_cloudy_with_heavy_rain.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Moderate rain"
            - precipMM : "4.7"
            - precipInches : "0.2"
            - humidity : "80"
            - visibility : "7"
            - visibilityMiles : "4"
            - pressure : "979"
            - pressureInches : "29"
            - cloudcover : "100"
            - heatIndexC : "10"
            - heatIndexF : "51"
            - dewPointC : "7"
            - dewPointF : "45"
            - windChillC : "7"
            - windChillF : "45"
            - windGustMiles : "25"
            - windGustKmph : "41"
            - feelsLikeC : "7"
            - feelsLikeF : "45"
            - chanceofrain : "77"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "92"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "3"
          ▿ 5 : Hourly
            - time : "1500"
            - tempC : "11"
            - tempF : "51"
            - windspeedMiles : "17"
            - windspeedKmph : "27"
            - winddirDegree : "257"
            - winddir16Point : "WSW"
            - weatherCode : "266"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Light drizzle"
            - precipMM : "0.5"
            - precipInches : "0.0"
            - humidity : "73"
            - visibility : "2"
            - visibilityMiles : "1"
            - pressure : "981"
            - pressureInches : "29"
            - cloudcover : "100"
            - heatIndexC : "11"
            - heatIndexF : "51"
            - dewPointC : "6"
            - dewPointF : "43"
            - windChillC : "8"
            - windChillF : "46"
            - windGustMiles : "23"
            - windGustKmph : "36"
            - feelsLikeC : "8"
            - feelsLikeF : "46"
            - chanceofrain : "60"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "89"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "3"
          ▿ 6 : Hourly
            - time : "1800"
            - tempC : "10"
            - tempF : "50"
            - windspeedMiles : "14"
            - windspeedKmph : "23"
            - winddirDegree : "253"
            - winddir16Point : "WSW"
            - weatherCode : "266"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Light drizzle"
            - precipMM : "0.3"
            - precipInches : "0.0"
            - humidity : "79"
            - visibility : "2"
            - visibilityMiles : "1"
            - pressure : "984"
            - pressureInches : "30"
            - cloudcover : "99"
            - heatIndexC : "10"
            - heatIndexF : "50"
            - dewPointC : "7"
            - dewPointF : "44"
            - windChillC : "7"
            - windChillF : "45"
            - windGustMiles : "20"
            - windGustKmph : "32"
            - feelsLikeC : "7"
            - feelsLikeF : "45"
            - chanceofrain : "65"
            - chanceofremdry : "0"
            - chanceofwindy : "0"
            - chanceofovercast : "90"
            - chanceofsunshine : "0"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "0"
          ▿ 7 : Hourly
            - time : "2100"
            - tempC : "9"
            - tempF : "49"
            - windspeedMiles : "13"
            - windspeedKmph : "20"
            - winddirDegree : "256"
            - winddir16Point : "WSW"
            - weatherCode : "122"
            ▿ weatherIconURL : 1 element
              ▿ 0 : Weather
                - value : "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
            ▿ weatherDesc : 1 element
              ▿ 0 : Weather
                - value : "Overcast"
            - precipMM : "0.0"
            - precipInches : "0.0"
            - humidity : "82"
            - visibility : "10"
            - visibilityMiles : "6"
            - pressure : "986"
            - pressureInches : "30"
            - cloudcover : "100"
            - heatIndexC : "9"
            - heatIndexF : "49"
            - dewPointC : "7"
            - dewPointF : "44"
            - windChillC : "7"
            - windChillF : "44"
            - windGustMiles : "18"
            - windGustKmph : "29"
            - feelsLikeC : "7"
            - feelsLikeF : "44"
            - chanceofrain : "0"
            - chanceofremdry : "85"
            - chanceofwindy : "0"
            - chanceofovercast : "84"
            - chanceofsunshine : "14"
            - chanceoffrost : "0"
            - chanceofhightemp : "0"
            - chanceoffog : "0"
            - chanceofsnow : "0"
            - chanceofthunder : "0"
            - uvIndex : "0"
    ▿ climateAverages : 1 element
      ▿ 0 : ClimateAverage
        ▿ month : 12 elements
          ▿ 0 : Month
            - index : "1"
            - name : "January"
            - avgMinTemp : "1.9"
            - avgMinTempF : "35.4"
            - avgMaxTemp : "6.5"
            - avgMaxTempF : "43.7"
            - absMinTemp : "-0.8"
            - absMinTempF : "30.5"
            - absMaxTemp : "8.614225"
            - absMaxTempF : "47.5"
            - avgTemp : "4.1"
            - avgTempF : "39.4"
            - maxWindSpeedKmph : "17.1"
            - maxWindSpeedMph : "10.6"
            - maxWindSpeedKnots : "9.0"
            - maxWindSpeedMS : "4.8"
            - avgWindSpeedKmph : "16.6"
            - avgWindSpeedMph : "10.3"
            - avgWindSpeedKnots : "8.0"
            - avgWindSpeedMS : "4.6"
            - avgWindGustKmph : "27.2"
            - avgWindGustMph : "16.9"
            - avgWindGustKnots : "14.0"
            - avgWindGustMS : "7.6"
            - avgDailyRainfall : "0.62"
            - avgDailyRainfallInch : "0.02"
            - avgMonthlyRainfall : "19.23"
            - avgMonthlyRainfallInch : "0.76"
            - avgHumidity : "87.74184"
            - avgCloud : "57.86419"
            - avgVisKM : "8.6"
            - avgVisMiles : "5.0"
            - avgPressureMB : "1014.1"
            - avgPressureInch : "30.4"
            - avgDryDays : "17"
            - avgRainDays : "11"
            - avgsnowDays : "2"
            - avgFogDays : "6"
            - avgThunderDays : "2"
            - avgUVIndex : "2"
            - avgSunHour : "124.8"
          ▿ 1 : Month
            - index : "2"
            - name : "February"
            - avgMinTemp : "2.4"
            - avgMinTempF : "36.3"
            - avgMaxTemp : "7.4"
            - avgMaxTempF : "45.3"
            - absMinTemp : "-0.1"
            - absMinTempF : "31.9"
            - absMaxTemp : "11.77857"
            - absMaxTempF : "53.2"
            - avgTemp : "4.7"
            - avgTempF : "40.5"
            - maxWindSpeedKmph : "17.2"
            - maxWindSpeedMph : "10.7"
            - maxWindSpeedKnots : "9.0"
            - maxWindSpeedMS : "4.8"
            - avgWindSpeedKmph : "16.7"
            - avgWindSpeedMph : "10.4"
            - avgWindSpeedKnots : "9.0"
            - avgWindSpeedMS : "4.7"
            - avgWindGustKmph : "26.5"
            - avgWindGustMph : "16.4"
            - avgWindGustKnots : "14.0"
            - avgWindGustMS : "7.4"
            - avgDailyRainfall : "0.76"
            - avgDailyRainfallInch : "0.03"
            - avgMonthlyRainfall : "21.25"
            - avgMonthlyRainfallInch : "0.84"
            - avgHumidity : "85.07125"
            - avgCloud : "60.92499"
            - avgVisKM : "8.9"
            - avgVisMiles : "5.0"
            - avgPressureMB : "1014.1"
            - avgPressureInch : "30.4"
            - avgDryDays : "16"
            - avgRainDays : "10"
            - avgsnowDays : "3"
            - avgFogDays : "6"
            - avgThunderDays : "1"
            - avgUVIndex : "2"
            - avgSunHour : "100.5"
gouyumei 回答:如何使用Swift 4和codable从复杂的json结构访问项目以获取天气

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3024190.html

大家都在问