swift 网络json解析 第三方请求数据

第三方只导入工程

import Alamofire
import UIKit

class FourVC: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    var tableView = UITableView()
    var dic:NSDictionary?
    var dictr:NSDictionary = [:]
    
    var dic1:NSArray = []

    
    var imgUrl:String = ""
    var dicLocal:NSDictionary = [:]

    

    override func viewDidLoad() {
        super.viewDidLoad()
       
        
   tableView = UITableView.init(frame: self.view.frame, style: .plain)
        
       
        
        tableView.delegate=self
        tableView.dataSource=self
        tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(tableView)
        // Do any additional setup after loading the view.
        Alamofire.request("http://live.ximalaya.com/live-web/v4/homepage?device=iPhone", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if(response.error == nil){
                print("请求成功")
                print(response.result.value as Any)
                self.dic = response.result.value as? NSDictionary
                
                self.dictr = self.dic?.object(forKey: "data") as! NSDictionary
                
                
                self.dic1 = self.dictr.object(forKey: "localRadios") as! NSArray
                for i in 0...2{
                self.dicLocal = self.dic1[i] as! NSDictionary
                     self.imgUrl = self.dicLocal.object(forKey: "coverLarge") as!String
                    
                }
               
                print(self.imgUrl);
            }else{
                print("请求失败\(String(describing: response.error))")
            }
            
            //解析网址
            self.tableView.reloadData()
            
            
        }
       
      
        
       
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return self.dic1.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let name:NSDictionary = dic1[indexPath.row] as! NSDictionary
        let cell :TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        cell.textLabel?.text=name["name"] as? String
        
        if let url = NSURL(string: self.imgUrl) {
            if let data = NSData(contentsOf: url as URL){
                 cell.imageView?.image=UIImage(data: data as Data)
                //  self.imgV.image = img
            }
        }
        
       
        
        cell.accessoryType = UITableViewCell.AccessoryType(rawValue: 1)!
        return cell;
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值