Swift,ObjectMapper:嵌套数组的路径
我有来自的json,它看起来像这样:Swift,ObjectMapper:嵌套数组的路径,swift,Swift,我有来自的json,它看起来像这样: { "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_m
{
"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映射到模型,AlamoFileObject Mapper扩展从请求而不是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())
}
}
有什么办法可以让它工作吗。
提前谢谢 您正在寻找的是:
let jsonDict = // You dict
jsonDict["weather"][0]["description"]
我只是给你一个方向。您需要将其与Swift类型转换规则对齐。祝你好运 我已经分叉并添加了这个功能,多亏了Tristan Himmelman,它已经被合并了,所以现在你可以访问嵌套的数组元素,比如这个map[“weather.0.description”]你如何存储这个json?提供更多细节。给我们密码。如果您将其存储在字典中,您可以获得
description
asdict[“description”]
此功能现在位于ObjectMapperI的主分支中。我有一个类似的问题,但我不仅在0索引处有嵌套数组。您能告诉我如何解析此响应吗
let jsonDict = // You dict
jsonDict["weather"][0]["description"]