swift 官方获取JSON 数据方法

时间:2024-08-08 00:05:44

var url = NSURL(string: "http://www.weather.com.cn/data/sk/101120501.html")

var data = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingUncached, error: nil)

var json : AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(), error: nil)

var weatherinfo: AnyObject? = json.objectForKey("weatherinfo")

var city: AnyObject? = weatherinfo?.objectForKey("city")

//  println(city)

swiftyJSON

////        // var url = NSURL(string: API_HOST+"/"+LOGIN_API)

//////        var url = NSURL(string: "http://www.weather.com.cn/data/sk/101120501.html")

//////        let data = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingUncached, error: nil)

//////

//////        //  println(city)

//////        let jsonS = JSON(data: data!)

//////        println(jsonS)

//////        println(jsonS[0]["weatherinfo"].string)

//////        if let info = jsonS["weatherinfo"].string{

//////            println(info)

//////        }