// // JsonUtil.swift // naiyoup // // Created by C Auto on 2021/6/2. // import UIKit import HandyJSON class JsonUtil: NSObject { /** * Json转对象 */ static func jsonToModel(_ jsonStr:String,_ modelType:HandyJSON.Type) ->BaseModel { if jsonStr == "" || jsonStr.count == 0 { #if DEBUG print("jsonoModel:字符串为空") #endif return BaseModel() } return modelType.deserialize(from: jsonStr) as! BaseModel } static func LoginjsonToModel(_ jsonStr:String,_ modelType:HandyJSON.Type) -> LoginModel { if jsonStr == "" || jsonStr.count == 0 { #if DEBUG print("jsonoModel:字符串为空") #endif return LoginModel() } return modelType.deserialize(from: jsonStr) as! LoginModel } /** * Json转数组对象 */ static func jsonArrayToModel(_ jsonArrayStr:String, _ modelType:HandyJSON.Type) ->[BaseModel] { if jsonArrayStr == "" || jsonArrayStr.count == 0 { #if DEBUG print("jsonToModelArray:字符串为空") #endif return [] } var modelArray:[BaseModel] = [] let data = jsonArrayStr.data(using: String.Encoding.utf8) let peoplesArray = try! JSONSerialization.jsonObject(with:data!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] for people in peoplesArray! { modelArray.append(dictionaryToModel(people as! [String : Any], modelType)) } return modelArray } /** * 字典转对象 */ static func dictionaryToModel(_ dictionStr:[String:Any],_ modelType:HandyJSON.Type) -> BaseModel { if dictionStr.count == 0 { #if DEBUG print("dictionaryToModel:字符串为空") #endif return BaseModel() } return modelType.deserialize(from: dictionStr) as! BaseModel } static func NodeldictionaryToModel(_ dictionStr:[String:Any],_ modelType:HandyJSON.Type) -> NodelModel { if dictionStr.count == 0 { #if DEBUG print("dictionaryToModel:字符串为空") #endif return NodelModel() } return modelType.deserialize(from: dictionStr) as! NodelModel } /** * 对象转JSON */ static func modelToJson(_ model:BaseModel?) -> String { if model == nil { #if DEBUG print("modelToJson:model为空") #endif return "" } return (model?.toJSONString())! } /** * 对象转字典 */ static func modelToDictionary(_ model:BaseModel?) -> [String:Any] { if model == nil { #if DEBUG print("modelToJson:model为空") #endif return [:] } return (model?.toJSON())! } }