网络编程:Json解析

来源:互联网 发布:巨森网络 编辑:程序博客网 时间:2024/04/30 10:05

在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

                            })


                        }

                    }

                }

            }

        }



0 0