加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift基础之json解析

发布时间:2020-12-14 06:54:31 所属栏目:百科 来源:网络整理
导读: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() { supe
import UIKit

class 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)")
        }
    }
    
}

运行结果

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读