1

I am trying to build a simple weather app using OpenWeatherMap APIs in Swift 4. I can parse Json data in simple cases, but this one has a more complex structure.

This is the Json file the API returns.

{"coord":{"lon":144.96,"lat":-37.81},"weather":[{"id":520,"main":"Rain","description":"light intensity shower rain","icon":"09n"}],"base":"stations","main":{"temp":288.82,"pressure":1019,"humidity":100,"temp_min":288.15,"temp_max":289.15},"visibility":10000,"wind":{"speed":4.1,"deg":200},"clouds":{"all":90},"dt":1544284800,"sys":{"type":1,"id":9548,"message":0.5221,"country":"AU","sunrise":1544208677,"sunset":1544261597},"id":2158177,"name":"Melbourne","cod":200}

I created a few Struct(s) to get the Json data.

struct CurrentLocalWeather: Decodable {
    let base: String
    let clouds: Clouds
    let cod: Int
    let coord: Coord
    let dt: Int
    let id: Int
    let main: Main
    let name: String
    let sys: Sys
    let visibility: Int
    let weather: [Weather]
    let wind: Wind
}
struct Clouds: Decodable {
    let all: Int
}
struct Coord: Decodable {
    let lat: Double
    let lon: Double
}
struct Main: Decodable {
    let humidity: Int
    let pressure: Int
    let temp: Double
    let tempMax: Int
    let tempMin: Int
    private enum CodingKeys: String, CodingKey {
        case humidity, pressure, temp, tempMax = "temp_max", tempMin = "temp_min"
    }
}
struct Sys: Decodable {
    let country: String
    let id: Int
    let message: Double
    let sunrise: UInt64
    let sunset: UInt64
    let type: Int
}
struct Weather: Decodable {
    let description: String
    let icon: String
    let id: Int
    let main: String
}
struct Wind: Decodable {
    let deg: Int
    let speed: Double
}

To use those datas this is the code I wrote:

let url = "https://api.openweathermap.org/data/2.5/weather?q=melbourne&APPID=XXXXXXXXXXXXXXXX"
        let objurl = URL(string: url)

        URLSession.shared.dataTask(with: objurl!) {(data, response, error) in

            do {
                let forecast = try JSONDecoder().decode([CurrentLocalWeather].self, from: data!)
                for weather in forecast {
                    print(weather.name)
                }
            } catch {
                print("Error")
            }

        }.resume()

That should print the city name in the console. Unfortunately it prints Error.

  • 3
    **Never ever** print a meaningless literal string when catching `Codable` errors. Don't do that. Print the `error` instance. It tells you exactly what's wrong. – vadian Dec 08 '18 at 22:38
  • Copied! Here it is .typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil)) – Tommaso Pirola Dec 09 '18 at 07:52
  • 1
    The solution is in Sh_Khan's answer. (The error message says that) the root object is clearly a dictionary (note the `{}`) – vadian Dec 09 '18 at 07:54
  • I edited the code but I get an error. Look at my reply to his comment for more information. – Tommaso Pirola Dec 09 '18 at 08:05
  • 1
    Please read the answer **carefully**. Are there square brackets?? – vadian Dec 09 '18 at 08:52
  • I did it. No more square brackets but the issue is still there. – Tommaso Pirola Dec 09 '18 at 12:25
  • It's a **different** issue. Please **read** the error message. `Codable` errors are extremely descriptive. *CodingKeys(stringValue: "temp_max ... Parsed JSON number <290.15> does not fit in `Int`"* clearly states that the value for key `temp_max` (as well as `temp_min`) is a **Double**. By the way: You can omit the coding keys if you specify the `.convertFromSnakeCase` key decoding strategy. – vadian Dec 09 '18 at 12:30
  • Alright, it works. I'm sorry, I am super new to coding. However, it doesn't work if I want to print the elements below the CurrentLocalWeather struct. `keyNotFound(CodingKeys(stringValue: "deg", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"deg\", intValue: nil) (\"deg\").", underlyingError: nil ` I'm not sure about the meaning of this error. – Tommaso Pirola Dec 09 '18 at 14:24
  • `deg` is in the `Wind` struct: `forecast.wind.deg` – vadian Dec 09 '18 at 20:37
  • Thank you. I also noticed that everything is done in the Do-catch is executed after everything else in the viewDidLoad. How can I assign the forecast.name output to a variable? If do this assignment inside the do-catch loop, it says UILabel.text must be used from main thread only – Tommaso Pirola Dec 10 '18 at 12:26
  • Updating the UI in the `do` scope is absolutely the right place. To fix the main thread issue use `DispatchQueue`: `DispatchQueue.main.async { self.myLabel.text = .... }` – vadian Dec 10 '18 at 12:35
  • Okay perfect. Last question: what if I want to get information inside the weather struct? Inside the CurrentLocalWeather struct, weather is inside the square brackets. What does it mean? And how can I access those data? – Tommaso Pirola Dec 10 '18 at 13:22
  • `[]` represents an array. There can be multiple `weather` objects for example when using the forecast API. You need a loop (like in your question) to iterate the items. – vadian Dec 10 '18 at 13:25
  • This doesn't work. I'm not sure how to parse arrays. `do { let forecast = try JSONDecoder().decode([CurrentLocalWeather].self, from: data!) for weather in forecast { print(weather.main) } } catch { print(error) }` – Tommaso Pirola Dec 10 '18 at 13:35
  • For the last time: `CurrentLocalWeather` is **not** an array. Please learn the type stuff. JSON has only two collection types: array (`[]`) and dictionary(`{}`).You get `weather` simply with `forecast.weather` – vadian Dec 10 '18 at 13:38
  • Am I supposed to parse `[Weather]` instead of `CurrentLocalWeather`? – Tommaso Pirola Dec 10 '18 at 13:47
  • No, you have to start always on the top. – vadian Dec 10 '18 at 13:49
  • In this case `do { let forecast = try JSONDecoder().decode(CurrentLocalWeather.self, from: data!) for weather in forecast { print(forecast.weather) } } catch { print(error) }` I get Type 'CurrentLocalWeather' does not conform to protocol 'Sequence' – Tommaso Pirola Dec 10 '18 at 14:04
  • * sigh (*sospirando*) * No. `for aWeather in forecast.weather { print(aWeather.main) }` – vadian Dec 10 '18 at 14:07

1 Answers1

0

You need

let forecast = try JSONDecoder().decode(CurrentLocalWeather.self, from: data!)
print(forcast.name)

as the root is a dictionary not array

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • do { let forecast = try JSONDecoder().decode([CurrentLocalWeather].self, from: data!) print(forecast.name) } catch { print(error) } }.resume() I edited the code as you suggested but there is an error "Value of type '[CurrentLocalWeather]' has no member 'name'" – Tommaso Pirola Dec 09 '18 at 08:04
  • it's `CurrentLocalWeather.self` not `[CurrentLocalWeather].self` – Shehata Gamal Dec 09 '18 at 09:10
  • I removed the square brackets, but still... `dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "main", intValue: nil), CodingKeys(stringValue: "temp_max", intValue: nil)], debugDescription: "Parsed JSON number <290.15> does not fit in Int.", underlyingError: nil))` – Tommaso Pirola Dec 09 '18 at 12:21