1

I have json from http://openweathermap.org/, and it looks like this:

{
"coord": {
"lon": 4.85,
"lat": 45.75
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "cmc stations",
"main": {
"temp": 278.988,
"pressure": 985.88,
"humidity": 92,
"temp_min": 278.988,
"temp_max": 278.988,
"sea_level": 1032.68,
"grnd_level": 985.88
},
"wind": {
"speed": 1.8,
"deg": 355
},
"clouds": {
"all": 80
},
"dt": 1445249394,
"sys": {
"message": 0.0037,
"country": "FR",
"sunrise": 1445234548,
"sunset": 1445273273
},
"id": 2996944,
"name": "Lyon",
"cod": 200
}

I'm using Alamofire 3.0 for networking, ObjectMapper for mapping json to model, and AlamofireObjectMapper extension to get model objects from request instead of json. Now I need to get weather description, but don't know how to write path for it. Tried ["weather.0.description"], ["weather.$0.description"], but these are not working, and my weather description is nil.

Here is my model:

class WCurrentWeather: Mappable {
    var weatherDescription: String?
    var tempriture: Double?
    var clouds: Double?
    var rain: Double?
    var humidity: Double?
    var pressure: Double?
    var sunrise: NSDate?
    var sunset: NSDate?

    required init?(_ map: Map){

    }

    func mapping(map: Map) {
        weatherDescription <- map["weather.0.description"]
        tempriture <- map["main.temp"]
        clouds <- map["clouds.all"]
        rain <- map["rain.1h"]
        humidity <- map["main.humidity"]
        pressure <- map["main.pressure"]
        sunrise <- (map["sys.sunrise"], DateTransform())
        sunset <- (map["sys.sunset"], DateTransform())
    }

}

and my request:

Alamofire.request(.GET, URL, parameters: params)
            .responseObject { (response: WCurrentWeather?, error: ErrorType?) in
                completionHandler(response, error)
        }

Is there any way to get this working. Thanks in advance.

1
  • How do you store this json? Give more details. Give us code. If you store it in a Dictionary, you can get description as dict["description"] Commented Oct 19, 2015 at 10:25

2 Answers 2

1

I have forked ObjectMapper, and added this functionality, and thanks to Tristan Himmelman it is already merged, so now you can access to nested array elements like this map["weather.0.description"]

Sign up to request clarification or add additional context in comments.

2 Comments

This functionality is now in the master branch of ObjectMapper
I have a similar issue but I have nested arrays not only at 0 index can you please tell me how to parse this response 104.236.140.101/energizers/API-JSON/category-details.php
0

What you are looking for is:

let jsonDict = // You dict

jsonDict["weather"][0]["description"]

I am just giving you a direction. You would need to align it with Swift type casting rules. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.