码迷,mamicode.com
首页 > 编程语言 > 详细

swift解析json数据

时间:2015-04-02 10:20:44      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

 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 UIKit
10 
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 String
41         cityid.text = weatherinfo.objectForKey("cityid") as String
42         temp1.text = weatherinfo.objectForKey("temp1") as String
43         temp2.text = weatherinfo.objectForKey("temp2") as String
44         weather.text = weatherinfo.objectForKey("weather") as String
45         img1.text = weatherinfo.objectForKey("img1") as String
46         img2.text = weatherinfo.objectForKey("img2") as String
47         ptime.text = weatherinfo.objectForKey("ptime") as String
48     }
49 
50     @IBAction func updateWeather(sender: AnyObject) {
51         getWeather()
52     }
53 }

 

swift解析json数据

标签:

原文地址:http://www.cnblogs.com/lovecc/p/4386075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!