{
"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?
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
+100
Most helpful comment
What I want is define a basic model for a part of JSON data, no the whole JSON
The model like :
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.