import UIKitclass VIEwController: UIVIEwController { @IBOutlet weak var image: UIImageVIEw! @IBOutlet weak var tittle: UILabel! @IBOutlet weak var myDescription: UITextVIEw! var dataJsON = NSMutableData() overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let stringURL:Nsstring = "https://itunes.apple.com/es/RSS/topfreeapplications/limit=10/Json" //building NSURL let url = NSURL(string: stringURL as String) //building NSURLRequest let request = NSURLRequest(URL: url!) //connection let connection: NSURLConnection? = NSURLConnection(request: request,delegate: self) if (connection != nil){ print("Connecting...") dataJsON = NSMutableData() } else{ print("Connection Failed") } } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() } func connection(connection: NSURLConnection!,dIDFailWithError error: NSError!){ print("Error: \(error)") } func connection(connection: NSURLConnection!,dIDReceiveResponse response: NSURLResponse!){ print("Received response: \(response)") //restore data dataJsON.length = 0 } func connection(connection: NSURLConnection!,dIDReceiveData data:NSData!){ self.dataJsON.appendData(data) } func connectionDIDFinishLoading(connection: NSURLConnection!){ do { let dic:NSDictionary! = try NSJsONSerialization.JsONObjectWithData(dataJsON,options: NSJsONReadingOptions.MutableContainers) as? NSDictionary //get from JsON let top1: AnyObject = ((dic["Feed"] as! NSDictionary) ["entry"]! as! NSArray) [0] let imgJson: AnyObject = (top1["im:image"] as! NSArray) [2] let url = NSURL(string: imgJson.objectForKey("label") as! String) let data = NSData(contentsOfURL: url!) let img = UIImage(data: data!) image.image = img //get tittle and description let tit = (top1["Title"] as! NSDictionary) ["label"] as! Nsstring let desc = (top1["summary"] as! NSDictionary) ["label"] as! Nsstring tittle.text = tit as String myDescription.text = desc as String } catch { // failure print("Fetch Failed: \((error as NSError).localizedDescription)") } } }
运行结果
总结
以上是内存溢出为你收集整理的swift基础之json解析全部内容,希望文章能够帮你解决swift基础之json解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)