iOS 【Swift-ObjectMapper对象转换】

2021-08-12  本文已影响0人  NJ_墨

json字符串转对象

class NewRespModel:Mappable{
    required init?(map: Map) {
    }
    func mapping(map: Map) {
        result <- map["result"]
        statusCode <- map["code"]
        message <- map["message"]
    }
    
    var result: NewResultModel?
    var statusCode:Int?
    var message:String?
}

class NewResultModel: Mappable {
    required init?(map: Map) {
    }
    
    func mapping(map: Map) {
        protogene <- map["protogene"]
        id <- map["id"]
    }
    
    var protogene:[ProtoModel]?
    var id:String?
}

class ProtoModel:Mappable {

    required init?(map: Map) {
    }
    
    func mapping(map: Map) {
        bg_color <- map["bg_color"]
        sort <- map["sort"]
    }
    
    var bg_color:String?
    var sort:String?
}
let resp = NewRespModel(JSONString: reqJson)

json字符串转数组

class PlatformModel: NSObject, Mappable {
    required init?(map: Map) {
    }
    func mapping(map: Map) {
        social_name <- map["social_name"]
        icon_link <- map["icon_link"]
    }
    @objc var social_name: NSString?
    @objc var icon_link: NSString?
}
let results = Mapper<PlatformModel>().mapArray(JSONObject: reqDic["result"]) as NSArray?
上一篇下一篇

猜你喜欢

热点阅读