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資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.