{"results":[{"location":{"id":"WS0E9D8WN298","name":"广州","country":"CN","path":"广州,广州,广东,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"大雨","code":"15","temperature":"31"},"last_update":"2016-08-15T15:05:00+08:00"}]}
如何进行json解析
data =
{
"results": [
{
"location": {
"id": "WS0E9D8WN298",
"name": "广州",
"country": "CN",
"path": "广州,广州,广东,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": "+08:00"
},
"now": {
"text": "大雨",
"code": "15",
"temperature": "31"
},
"last_update": "2016-08-15T15:05:00+08:00"
}
]
}
guard let jsonObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) else {
return
}
let results = jsonObject["results"]
let info = NSDictionary()
for result in results {
let location = result["location"] as! NSDictionary
info.setValue(location["id"], forKey: "id")
info.setValue(location["name"], forKey: "name")
info.setValue(location["country"], forKey: "country")
info.setValue(location["path"], forKey: "path")
info.setValue(location["timezone"], forKey: "timezone")
info.setValue(location["timezone_offset"], forKey: "timezone_offset")
let now = result["now"] as! NSDictionary
info.setValue(now["text"], forKey: "text")
info.setValue(now["code"], forKey: "code")
info.setValue(now["temperature"], forKey: "temperature")
info.setValue(result["last_update"], forKey: "last_update")
}
print(info)