iOS开发——数据解析Swift篇&简单json数据处理

时间:2022-07-08 13:09:32

简单json数据处理

 //loadWeather

 var url = NSURL(string: "http://www.weather.com.cn/adat/sk/101240701.html")
 //提示没有这个方法?????????
 //var data = NSData.dataWithContentsOfURL(url!, options:NSDataReadingOptions.DataReadingUncached, error: nil)
 var data = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingUncached, error: nil)

 var json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil)
 /**
 var weatherInfo.....
     (.0 "cityid", .1 "101010100")
 (.0 "city", .1 "北京")
 (.0 "time", .1 "19:05")
 (.0 "isRadar", .1 "1")
 */
 /**
 {"weatherinfo":{"city":"赣州","cityid":"101240701","temp":"26","WD":"西南风","WS":"4级","SD":"63%","WSE":"4","time":"10:30","isRadar":"1","Radar":"JC_RADAR_AZ9797_JB","njd":"暂无实况","qy":"992"}}
 */
 //解析
 var weatherInfo: AnyObject? = json?.objectForKey("weatherinfo: AnyObject?")

 var city: AnyObject? = weatherInfo?.objectForKey("city")

 var temo: AnyObject? = weatherInfo?.objectForKey("temp")

 var wind: AnyObject? = weatherInfo?.objectForKey("WD")