Search code examples
jsonswiftopenweathermap

Swift weather data from Openweathermap API?


From openweathermap api, I am getting below response.

{
  "cod":"200",
  "message":0,
  "cnt":40,
  "list":[
     {
        "dt":1587643200,
        "main":{
           "temp":289.78,
           "feels_like":283.61,
           "temp_min":289.03,
           "temp_max":289.78,
           "pressure":1014,
           "sea_level":1014,
           "grnd_level":1010,
           "humidity":41,
           "temp_kf":0.75
        },
        "weather":[
           {
              "id":804,
              "main":"Clouds",
              "description":"overcast clouds",
              "icon":"04d"
           }
        ],
        "clouds":{
           "all":94
        },
        "wind":{
           "speed":6.75,
           "deg":2
        },
        "sys":{
           "pod":"d"
        },
        "dt_txt":"2020-04-23 12:00:00"
     },
     {
        "dt":1587654000,
        "main":{
           "temp":289.66,
           "feels_like":284.44,
           "temp_min":289.34,
           "temp_max":289.66,
           "pressure":1013,
           "sea_level":1013,
           "grnd_level":1009,
           "humidity":47,
           "temp_kf":0.32
        },
        "weather":[
           {
              "id":803,
              "main":"Clouds",
              "description":"broken clouds",
              "icon":"04d"
           }
        ],
        "clouds":{
           "all":67
        },
        "wind":{
           "speed":5.9,
           "deg":357
        },
        "sys":{
           "pod":"d"
        },
        "dt_txt":"2020-04-23 15:00:00"
     }

Then I write the code below to get wind data for any specific daytime(dt). I get the jsonresponse to the Any "list". But I can't get the wind data. I get the error

"Value of type 'Any' has no subscripts".

Also, I can't understand how can I get the wind data for dt=1587643200 and dt=1587654000 separately.

                           if let list = jsonresponse["list"] as? Any {
                               let wind = list["wind"] as? [String : Any],
                               print(wind)
                           }

Solution

  • This is a super simple example, this question is similar to your problem. I would like that you learn about Codable protocol to simplify and improve your code because of this way is super creepy.

    let url = URL(string: "https://samples.openweathermap.org/data/2.5/history/city?id=2885679&type=hour&appid=b1b15e88fa797225412429c1c50c122a1")!
    URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
        if let error = error {
            print(error.localizedDescription)
            return
        }
    
        guard let httpResponse = response as? HTTPURLResponse,
            (200...299).contains(httpResponse.statusCode) else {
                print("Error with the response, unexpected status code: \(String(describing: response))")
                return
        }
    
        guard let data = data else {
            return
        }
    
        guard let dictionaryObj = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
            return
        }
    
        guard let list = dictionaryObj["list"] as? [[String: Any]] else {
    
            return
        }
    
        if let first = list.first, let wind = first["wind"] {
            print(wind)
        }
    }).resume()