json文件

let mArr:NSMutableArray = []

//json文件k路径
let path = Bundle.main.path(forResource: “daren”, ofType: “json”)
//将json转换成URL
let url = URL(fileURLWithPath: path!)
//将url转换成data数据
do {
let data = try Data(contentsOf: url)
//把数据抓换成json
let jsonData = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers)
let arr:NSArray = jsonData as! NSArray
print(arr)

        for item in arr {
            
            let k:NSDictionary = item as! NSDictionary
            
            if k.object(forKey: "type")as!String == "0"{
                
                let md:OneMD = OneMD(headImage: k.object(forKey: "headImage") as! String, title: k.object(forKey: "titlt") as! String, time: k.object(forKey: "time") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                
                
                
                mArr.add(md)
                
            }else if k.object(forKey: "type")as!String == "1"{
                
                let md:TwoMD = TwoMD(headImage: k.object(forKey: "headImage") as! String, title: k.object(forKey: "titlt") as! String, time: k.object(forKey: "time") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! NSArray, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                
                mArr.add(md)
                
            }else{
                
                let md:ThreeMD = ThreeMD(headImage: k.object(forKey: "headImage") as! String, title: k.object(forKey: "titlt") as! String, time: k.object(forKey: "time") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                
                mArr.add(md)
                
            }
            
            
        }
        
        
        
        
    } catch let error as Error? {
        
    }

猜你喜欢

转载自blog.csdn.net/weixin_43656520/article/details/86551501