路徑我有一個從http://openweathermap.org/ JSON和它看起來像這樣:斯威夫特,ObjectMapper:嵌套陣列
{
"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
}
我使用Alamofire 3.0聯網,ObjectMapper映射JSON模型,並AlamofireObjectMapper擴展從請求中獲取模型對象而不是json。 現在我需要得到天氣描述,但不知道如何爲它寫路徑。試過[「weather.0.description」],[「weather。$ 0.description」],但這些都不起作用,我的天氣描述爲零。
這裏是我的模型:
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())
}
}
和我的要求:
Alamofire.request(.GET, URL, parameters: params)
.responseObject { (response: WCurrentWeather?, error: ErrorType?) in
completionHandler(response, error)
}
有沒有辦法得到這個工作。 在此先感謝。
怎麼辦你存儲這個JSON?提供更多細節。給我們代碼。如果你把它存儲在一個字典中,你可以將'description'作爲'dict [「description」]' – katleta3000