swift model 解析的两种方法

1. 常规解析方法

    //懒加载声明一个LJNewsModel为数据的数组

    lazy var ljArray : [LJNewsModel] = [LJNewsModel]()


//MARK:-- 数据获取和解析
extension NewsViewController{
    func requestNetData(){
        /*
         打印json数据
         */
        LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in
             方法一:解析数据
             
             let str = String(data:data!, encoding: String.Encoding.utf8)!
             print(str)
             
             let ljTempArray : NSArray = self.getDictionaryFromJSONString(jsonString: str).object(forKey: "T1348647909107") as! NSArray
             
             for m in 0 ..< ljTempArray.count
             {
             let dict:NSDictionary = ljTempArray[m] as! NSDictionary
             let model = LJNewsModel()
             model.imageUrl = dict.object(forKey: "imgsrc") as! String
             model.contentStr = dict.object(forKey: "title") as! String
             let count :Int = (dict.object(forKey: "replyCount") != nil) ?  dict.object(forKey: "replyCount") as! Int : 0
             model.replyCount = "\(count)"
             self.ljArray.add(model)
             }
            
            self.ljTablewView.reloadData()
        }
    }
    
    func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
        
        let jsonData:Data = jsonString.data(using: .utf8)!
        
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
}

model

import Foundation

class LJNewsModel: NSObject {
    
    //MARK:-  定义属性
    var imgsrc: String = ""    ///< store user's name, optional
    var title: String = ""      ///< store user's telephone number
    var replyCount: Int = 0
    
    //方法二的model
    override init() {
        super.init()
    }
    
    func setModel(_ imageUrl: String ,_ contentStr: String, _ replyCount:Int) {
        self.imageUrl = imageUrl
        self.contentStr = contentStr
        self.replyCount = replyCount
    }
}

2. 利用swift自有的函数进行解析-------推荐

//MARK:-- 数据获取和解析
extension NewsViewController{
    func requestNetData(){
        /*
         打印json数据
         */
        LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in
            
            //as? [[String :Any]]  转化为以字典为元素的数组
            //as? [String :Any]    转化为字典
            //1.方法一:解析数据 -- 推荐
            let str = String(data:data!, encoding: String.Encoding.utf8)!
            guard let allResulrDict = self.getDictionaryFromJSONString(jsonString:str) as? [String : Any] else {return}
            guard let dataArray = allResulrDict["T1348647909107"] as? [[String :Any]] else {return}
            //print(dataArray)
            
            for dict in dataArray{
                self.ljArray.append(LJNewsModel(dict))
            }
            self.ljTablewView.reloadData()
        }
    }
    
    func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
        
        let jsonData:Data = jsonString.data(using: .utf8)!
        
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
}

import Foundation

class LJNewsModel: NSObject {
    
    //MARK:-  定义属性
    var imgsrc: String = ""    ///< store user's name, optional
    var title: String = ""      ///< store user's telephone number
    var replyCount: Int = 0
    
     //方法一的model
    //MARK:-  自定义构造函数
    init(_ dict : [String: Any]){
       super.init()
        setValuesForKeys(dict)
    }

    override func setValue(_ value: Any?, forUndefinedKey key: String) {
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值