I’m struggling cause I don’t know how to consume this JSON because in the "weather" part of the JSON it receives [0] as key-value and I don’t know how to declare it
{
"coord": {
"lon": -35.7353,
"lat": -9.6658
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
],
"base": "stations",
"main": {
"temp": 302.84,
"feels_like": 305.59,
"temp_min": 302.84,
"temp_max": 302.84,
"pressure": 1011,
"humidity": 61
},
"visibility": 10000,
"wind": {
"speed": 7.2,
"deg": 90
},
"clouds": {
"all": 20
},
"dt": 1672689129,
"sys": {
"type": 1,
"id": 8413,
"country": "BR",
"sunrise": 1672646788,
"sunset": 1672692412
},
"timezone": -10800,
"id": 3395981,
"name": "MaceiĆ³",
"cod": 200
}
my attempt looked like this, and it kept failing cause of the weather part. i’ve tried some stuff like UUID() but none of it seens to work (maybe i’m applying it the wrong way)
import Foundation
struct Response: Codable {
var coord: Coord
var weather: [Weather]?
var main: Main
var visibility: Int
var wind: Wind
var rain: Rain?
var clouds: Clouds?
var dt: Int
var sys: Sys
var timezone: Int
var id: Int
var name: String
var cod: Int
}
struct Coord: Codable {
var lon: Double
var lat: Double
}
struct WeatherBase: Codable {
var id: String { _id }
private var _id: String
var base: String
}
struct Weather: Codable {
var id: Int
var main: String
var description: String
var icon: String
}
struct Main: Codable {
var temp: Double
var feels_like: Double
var temp_min: Double
var temp_max: Double
var pressure: Int
var humidity: Int
}
struct Wind: Codable {
var speed: Double
var deg: Int
}
struct Rain: Codable{
var umh: Int
}
struct Clouds: Codable {
var all: Int
}
struct Sys: Codable {
var type: Int
var id: Int
var country: String
var sunrise: Int
var sunset: Int
}
2
Answers
I couldn’t reproduce your error using your code and json object. I just remove the WeatherBase struct because it was not used in any part.
I open a playground to understand better you problem and add the following code.
Result
I hope I`ve helped you.
As I mentioned in my answer to your previous question,
you need to check the docs to determine which fields are optional
and amend your code accordingly. Note the extra
var base: String
property in theResponse
struct.The following
SwiftUi
code shows how to access the datathat is retrieved from the
openweathermap
server, in particular the weather info.Use something similar for your UIKit code.
Note that you must check the index before you access the data, as shown, otherwise your app will crash.