/ Dados de resposta / JSON em objetos swift [ObjectMapper] - json, swift, objectmapper

Dados de resposta JSON em objetos swift [ObjectMapper] - json, swift, objectmapper

Vocês podem me ajudar a criar objetos rápidos usando o ObjectMapper de seguir dados JSON?

[{
"location": "Toronto, Canada",
"three_day_forecast": [
{
"conditions": "Partly cloudy",
"day" : "Monday",
"temperature": 20
},
{
"conditions": "Showers",
"day" : "Tuesday",
"temperature": 22
},
{
"conditions": "Sunny",
"day" : "Wednesday",
"temperature": 28
}
]
}
]

Respostas:

3 para resposta № 1

Se você estiver usando ObjectMapper:

import ObjectMapper

struct WeatherForecast: Mappable {
var location = ""
var threeDayForecast = [DailyForecast]()

init?(_ map: Map) {
// Validate your JSON here: check for required properties, etc
}

mutating func mapping(map: Map) {
location            <- map["location"]
threeDayForecast    <- map["three_day_forecast"]
}
}

struct DailyForecast: Mappable {
var conditions = ""
var day = ""
var temperature = 0

init?(_ map: Map) {
// Validate your JSON here: check for required properties, etc
}

mutating func mapping(map: Map) {
conditions      <- map["conditions"]
day             <- map["day"]
temperature     <- map["temperature"]
}
}

Uso:

// data is whatever you get back from the web request
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: [])
let forecasts = Mapper<WeatherForecast>().mapArray(json)