Swiftmodel解析的方法有哪些
这篇文章主要介绍Swift model解析的方法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联建站自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站建设、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元田东做网站,已为上家服务,为田东各地企业和个人服务,联系电话:028-86922220
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) { } }
以上是“Swift model解析的方法有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
新闻标题:Swiftmodel解析的方法有哪些
标题URL:http://azwzsj.com/article/pejgce.html