swift解析json資料

來源:互聯網
上載者:User

標籤:

 1 // 2 //  ViewController.swift 3 //  JieParseJson 4 // 5 //  Created by jiezhang on 14-10-5. 6 //  Copyright (c) 2014年 jiezhang. All rights reserved. 7 // 8  9 import UIKit10 11 class ViewController: UIViewController {12                             13     @IBOutlet weak var city: UILabel!14     @IBOutlet weak var cityid: UILabel!15     @IBOutlet weak var temp1: UILabel!16     @IBOutlet weak var temp2: UILabel!17     @IBOutlet weak var weather: UILabel!18     @IBOutlet weak var img1: UILabel!19     @IBOutlet weak var img2: UILabel!20     @IBOutlet weak var ptime: UILabel!21     22     23     override func viewDidLoad() {24         super.viewDidLoad()25     }26 27     override func didReceiveMemoryWarning() {28         super.didReceiveMemoryWarning()29         30     }31     32     func getWeather()33     {34         var url = NSURL(string: "http://www.weather.com.cn/data/cityinfo/101010100.html")35         var data = NSData(contentsOfURL: url)36         var str = NSString(data: data, encoding: NSUTF8StringEncoding)37         println(str)38         var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)39         let weatherinfo: AnyObject = json.objectForKey("weatherinfo")!40         city.text = weatherinfo.objectForKey("city") as String41         cityid.text = weatherinfo.objectForKey("cityid") as String42         temp1.text = weatherinfo.objectForKey("temp1") as String43         temp2.text = weatherinfo.objectForKey("temp2") as String44         weather.text = weatherinfo.objectForKey("weather") as String45         img1.text = weatherinfo.objectForKey("img1") as String46         img2.text = weatherinfo.objectForKey("img2") as String47         ptime.text = weatherinfo.objectForKey("ptime") as String48     }49 50     @IBAction func updateWeather(sender: AnyObject) {51         getWeather()52     }53 }

 

swift解析json資料

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.