Visit the following link to get the weather of the capital day: http://www.weather.com.cn/adat/sk/101010100.html
The JSON format returned is as follows:
{"Weatherinfo": {"City": "Beijing", "Cityid": "101010100", "temp": "9", "WD": "Southwest Wind", "WS": "Level 2", "SD": "22%", "WSE": "2", "Time": " 10:35 "," Isradar ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," NJD ":" No Live "," qy ":" 1015 "}}
OK, so I'll build a playground and typing the following code, without setting all the errors to 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)
Parsing JSON data
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
The following message is printed on the right side of playground:
Swift parsing weather JSON format