viewcontroller.swift//jieparsejson////Created by Jiezhang on 14-10-5.//Copyright (c) 2014 Jiezhang. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller {@IBOutlet Weak V Ar 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.di Dreceivememorywarning ()} 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, Encod ing:nsutf8stringencoding) println (str) var json:anyobject! = Nsjsonserialization.jsonoBjectwithdata (data, options:NSJSONReadingOptions.AllowFragments, error:nil) Let Weatherinfo:anyobject = Json.obj Ectforkey ("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 St Ring 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 (" p Time ") as String} @IBAction func Updateweather (sender:anyobject) {GetWeather ()}}
I wanted to show the picture, but the returned picture was fake, I looked at the page,<big class= "jpg80 n01" ></big> 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: http://i.tq121.com.cn/i/weather2014/jpg/blue80.jpg, opened after a group of pictures, like the following, on the site is to see the CSS cut out.
Swift Basics-Parsing server JSON content