swift加载本地json文件

let path = Bundle.main.path(forResource: "LiteratureModel", ofType: ".json")
//        let url = URL(fileURLWithPath: path!)
//        do {
//            let data = try Data(contentsOf: url)
//            let jsonDic:Dictionary<String,Any> = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, Any>
//            let m: YXLiteratureTotalModel = Mapper<YXLiteratureTotalModel>().map(JSON: jsonDic)!
//            self.literatureCurrentModel = m;
//            
//        } catch let error as Error? {
//            print("读取本地数据出现错误!",error as Any)
//        }



其中YXLiteratureTotalModel.swift
import UIKit
import ObjectMapper

class YXLiteratureTotalModel: Mappable {

    var note:String?
    var category: String?
    var topic: String?
    
    required init?(map: Map) {}
    func mapping(map: Map) {
        
        note       <-  map["note"]
        category   <-  map["category"]
        topic      <-  map["topic"]
    }
}
发布了337 篇原创文章 · 获赞 25 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/u012581760/article/details/89638867