網絡編程:Json解析

k780.com上獲取數據:

1.生成二維碼:

let url = NSURL(string: "http://api.k780.com:88/?app=qr.get&data=testqqqq&level=L&size=6")!

        

        let request = NSURLRequest(URL: url)

        

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (_, data, error) -> Void in


            if error == nil {

                

                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    self.qrcodeImageView.image = UIImage(data: data)

                })

            }


        }


2.獲取天氣數據:

let weatherRequest = NSURLRequest(URL: NSURL(string: "http://api.k780.com:88/? app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json)!)

        NSURLConnection.sendAsynchronousRequest(weatherRequest, queue: NSOperationQueue()) { (_, data, error) - > Void in

            if error == nil {


                if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? NSDictionary {

                    if let result = json.valueForKey("result") as? NSArray {

                        if let resultToday = result[0] as? NSDictionary{

                            

                            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                                self.locationLable.text = resultToday["citynm"] as? String

                                self.weatherLable.text = resultToday["weather"] as? String

                                self.tempLable.text = resultToday["temperature"] as? String

                            })


                        }

                    }

                }

            }

        }



發佈了53 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章