标签:style blog http color io os 使用 ar for
//
// ViewController.swift
// http_request
//
// Created by andy on 14-9-6.
// Copyright (c) 2014年 andy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var btn:UIButton?
@IBOutlet var lable:UILabel?
override func viewDidLoad() {
super.viewDidLoad()
//TextField
}
@IBAction func btnClick(AnyObject) {
// ss()
loadJsonData()
}
//解析JSON的方法
func loadJsonData(){
//定义获取json数据的接口地址,这里定义的是获取天气的API接口,还有一个好处,就是swift语句可以不用强制在每条语句结束的时候用";"
var url = NSURL(string:"http://www.weather.com.cn/data/sk/101110101.html")
//获取JSON数据
var data = NSData.dataWithContentsOfURL(url,options:NSDataReadingOptions.DataReadingUncached,error:nil)
var json:AnyObject = NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.AllowFragments,error:nil)!
//解析获取JSON字段值
var weatherInfo:AnyObject = json.objectForKey("weatherinfo")! //json结构字段名。
var city:AnyObject = weatherInfo.objectForKey("city")!
//以此类推获取其他字段的信息,再此我就不过多赘述了。
//然后把获取到的json值赋值个相应的控件即可。
//tv.text = "城市:\(city)" //使用\()可以将变量嵌入到相应的字符串中,
println(city)
}
func ss() {
let urlPath: String = "http://www.weather.com.cn/data/sk/101010100.html"
var url: NSURL = NSURL(string: urlPath)
var request1: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
/* Your code */
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println("AsSynchronous\(jsonResult)")
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/xyc20080413/p/3980333.html