码迷,mamicode.com
首页 > 编程语言 > 详细

SWIFT解析天气JSON格式

时间:2015-05-03 21:53:53      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

访问以下链接可以得到京城当天的天气:http://www.weather.com.cn/adat/sk/101010100.html

返回的JSON格式如下:

{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"9","WD":"西南风","WS":"2级","SD":"22%","WSE":"2","time":"10:35","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1015"}}

好吧,接下来我就建立一个playground并敲入以下代码,暂无把所有的错误设为nil:

var url = NSURL(string: "http://www.weather.com.cn/adat/sk/101010100.html")

var data = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingUncached, error: nil)

var json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil)

 

//解析JSON数据

var weatherinfo:AnyObject? = json?.objectForKey("weatherinfo")

var cityId:NSString = weatherinfo?.objectForKey("cityid") as NSString

var city:NSString = weatherinfo?.objectForKey("city") as NSString

var wind:NSString = weatherinfo?.objectForKey("WD") as NSString

 

playground右边会打印出如下消息:

技术分享

 

SWIFT解析天气JSON格式

标签:

原文地址:http://www.cnblogs.com/foxting/p/4474727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!