Objectmapper: How to specify a Key Path?

Created on 17 Nov 2016  路  5Comments  路  Source: tristanhimmelman/ObjectMapper

{ "data": [ { "type": 1, "portraitimgurl": "/upload/product/firstlevel/shushangzhuang.jpg", "landscapeimgurl": "/upload/product/firstlevel/henshangzhuang.jpg", "count": 0 }, { "type": 2, "portraitimgurl": "/upload/product/firstlevel/shuqunzhuang.jpg", "landscapeimgurl": "/upload/product/firstlevel/henqunzhuang.jpg", "count": 0 }, { "type": 3, "portraitimgurl": "/upload/product/firstlevel/shukuzhuang.jpg", "landscapeimgurl": "/upload/product/firstlevel/henkuzhuang.jpg", "count": 0 } ], "errcode": 0, "errmsg": "ok" }
I just wanna get model array from "data", which function should I use?

Most helpful comment

What I want is define a basic model for a part of JSON data, no the whole JSON

{
    "type": 1,
    "portraitimgurl": "/upload/product/firstlevel/shushangzhuang.jpg",
    "landscapeimgurl": "/upload/product/firstlevel/henshangzhuang.jpg",
    "count": 0
}

The model like :

class ImageModel: Mappable {
    var type: Int?
    var portraitimgurl: String?
    var landscapeimgurl: String?
    var count: Int?

    init?(_ map: Map){}

    func mapping(){
        type <- map["type"]
        portraitimgurl <- map["portraitimgurl"]
        landscapeimgurl <- map["landscapeimgurl"]
        count <- map["count"]
    }
}

So, if i have a complicated JSON, i just need to define a simple model, not a lot of nested model.
I think keyPath is a very common usage for JSONModel, i am looking forward to this new feature.

All 5 comments

You just need to create a model object that maps type, the image urls and the count. THen you can map the whole response with something like the following :

class Response: Mappable {
    var data: [String: Model]?
    init?(_ map: Map){}

    func mapping(){
        data <- map["data"]
    }
}

What I want is define a basic model for a part of JSON data, no the whole JSON

{
    "type": 1,
    "portraitimgurl": "/upload/product/firstlevel/shushangzhuang.jpg",
    "landscapeimgurl": "/upload/product/firstlevel/henshangzhuang.jpg",
    "count": 0
}

The model like :

class ImageModel: Mappable {
    var type: Int?
    var portraitimgurl: String?
    var landscapeimgurl: String?
    var count: Int?

    init?(_ map: Map){}

    func mapping(){
        type <- map["type"]
        portraitimgurl <- map["portraitimgurl"]
        landscapeimgurl <- map["landscapeimgurl"]
        count <- map["count"]
    }
}

So, if i have a complicated JSON, i just need to define a simple model, not a lot of nested model.
I think keyPath is a very common usage for JSONModel, i am looking forward to this new feature.

+1

//data is result from Alamofire
let dataJSON = data.value(forKeyPath: "data") as? [Dictionary] ?? []

let imageModelArray = Mapper().mapArray(JSONArray: dataJSON)

+100

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YunyueLin picture YunyueLin  路  3Comments

zhengying picture zhengying  路  4Comments

amg1976 picture amg1976  路  3Comments

nearspears picture nearspears  路  4Comments

Dbigshooter picture Dbigshooter  路  4Comments