Swift basics-parsing server Json content
//// ViewController. swift // JieParseJson /// Created by jiezhang on 14-10-5. // Copyright (c) jiezhang. all rights reserved. // import UIKitclass ViewController: UIViewController {@ IBOutlet weak var city: UILabel! @ IBOutlet weak var cityid: UILabel! @ IBOutlet weak var temp1: UILabel! @ IBOutlet weak var temp2: UILabel! @ IBOutlet weak var weather: UILabel! @ IBOutlet weak var img1: UILabel! @ IBOutlet weak var img2: UILabel! @ IBOutlet weak var ptime: UILabel! Override func viewDidLoad () {super. viewDidLoad ()} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning ()} func getWeather () {var url = NSURL (string: "http://www.weather.com.cn/data/cityinfo/101010100.html") var data = NSData (contentsOfURL: url) var str = NSString (data: data, encoding: NSUTF8StringEncoding) println (str) var json: AnyObject! = NSJSONSerialization. JSONObjectWithData (data, options: NSJSONReadingOptions. AllowFragments, error: nil) let weatherinfo: AnyObject = json. objectForKey ("weatherinfo ")! City. text = weatherinfo. objectForKey ("city") as String cityid. text = weatherinfo. objectForKey ("cityid") as String temp1.text = weatherinfo. objectForKey ("temp1") as String temp2.text = weatherinfo. objectForKey ("temp2") as String weather. text = weatherinfo. objectForKey ("weather") as String img1.text = weatherinfo. objectForKey ("img1") as String img2.text = weatherinfo. objectForKey ("img2") as String ptime. text = weatherinfo. objectForKey ("ptime") as String} @ IBAction func updateWeather (sender: AnyObject) {getWeather ()}}
At that time, I wanted to display images, but the returned images were fake. I checked the page,This is the case in css.
big.jpg80{background-image:url(../../i/weather2014/jpg/gray80.jpg);height:80px;width:80px}.on big.jpg30{background-image:url(../../i/weather2014/jpg/white30.jpg);height:30px;width:31px}.on big.jpg50{background-image:url(../../i/weather2014/jpg/blue50.jpg);height:50px;width:50px}.on big.jpg80{background-image:url(../../i/weather2014/jpg/blue80.jpg);height:80px;width:80px}.sk big.jpg80{background-image:url(../../i/weather2014/jpg/blue80.jpg);height:80px;width:80px}
So the real address: Success.