Cowboy Tech

iOS项目20-Swift实现PM2.5显示

func loadPM25Data(){

msgLabel.text = "Loding..."

    NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: NSURL(string:"http://aqicn.org/publishingdata/json")!), queue: NSOperationQueue.mainQueue()) { (resp:NSURLResponse?, data:NSData?, error:NSError?) in

        do {let jsonArr = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
        let firstData = (jsonArr as! NSArray).objectAtIndex(0)
        let pollutants = firstData.valueForKey("pollutants")
            let pm2_5 = (pollutants as! NSArray).objectAtIndex(0)
            let theValue = pm2_5.valueForKey("value")

            self.msgLabel.text = "PM2.5:\(theValue!)"


        }catch{

        print("Hello")
        }
    }
}