詳解Swift model 解析的兩種方法
詳解Swift model 解析的兩種方法
1. 常規(guī)解析方法
//懶加載聲明一個(gè)LJNewsModel為數(shù)據(jù)的數(shù)組 lazy var ljArray : [LJNewsModel] = [LJNewsModel]()
//MARK:-- 數(shù)據(jù)獲取和解析
extension NewsViewController{
func requestNetData(){
/*
打印json數(shù)據(jù)
*/
LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in
方法一:解析數(shù)據(jù)
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自有的函數(shù)進(jìn)行解析-------推薦
//MARK:-- 數(shù)據(jù)獲取和解析
extension NewsViewController{
func requestNetData(){
/*
打印json數(shù)據(jù)
*/
LJDownLoadNetImage.request("GET", url: "http://c.m.163.com/nc/article/list/T1348647909107/0-20.html") { (data, respond, error) in
//as? [[String :Any]] 轉(zhuǎn)化為以字典為元素的數(shù)組
//as? [String :Any] 轉(zhuǎn)化為字典
//1.方法一:解析數(shù)據(jù) -- 推薦
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:- 自定義構(gòu)造函數(shù)
init(_ dict : [String: Any]){
super.init()
setValuesForKeys(dict)
}
override func setValue(_ value: Any?, forUndefinedKey key: String) {
}
}
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例
本篇文章主要介紹了iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例
本篇文章主要介紹了iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
淺談RxSwift 網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了淺談RxSwift 網(wǎng)絡(luò)請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
ios動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù)的區(qū)別
這篇文章主要介紹了ios動(dòng)態(tài)庫(kù)和靜態(tài)庫(kù)的區(qū)別,幫助大家更好的理解和學(xué)習(xí)使用ios開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04
iOS實(shí)現(xiàn)微信/QQ顯示最近拍攝圖片的功能實(shí)例代碼
如果你剛剛拍攝了圖片,在使用微信/QQ發(fā)生消息時(shí)會(huì)顯示“你可能要發(fā)送的圖片”,這個(gè)功能非常人性化,怎么實(shí)現(xiàn)的呢?下面小編給大家分享iOS實(shí)現(xiàn)微信/QQ顯示最近拍攝圖片的功能實(shí)例代碼,一起看看吧2017-03-03
iOS中FMDB事務(wù)實(shí)現(xiàn)批量更新數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了iOS中FMDB事務(wù)實(shí)現(xiàn)批量更新數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

