I have a JSON as
{
"code": 200,
"status": "success",
"data": [
{
"cardTypeId": 1,
"cardInfo": {
"background": "#4267b2",
"userName": "abkur_rt",
"text": "Hello Video",
"media": {
"mediaUrl": "",
"mediaType": "image",
"mediaThumbUrl": ""
}
}
},
{
"cardTypeId": 4,
"cardInfo": {
"text": "Image and text",
"media": {
"mediaUrl": "",
"mediaType": "image",
"mediaThumbUrl": ""
}
}
}
]
}
To Parse this i used ObjectMapper(https://github.com/tristanhimmelman/ObjectMapper)
My query is that in my JSON i get cardInfo
depending upon the cardTypeId
So I made classes referring this link ObjectMapper how to map different object based on JSON to understand how to make use of custom TransformType for classes. In the link's JSON reponse has Array but in my case if cardTypeId
is 1 then there is 2 fields extra where as everything same in cardInfo
. So i have made classes as below but i am not sure how will i create class inheriting TransFormType.
class LBDetailsList: Mappable {
var lbListArray : [LBDetail]?
required init?(map: Map) {
}
func mapping(map: Map) {
lbListArray <- map ["data"]
}
}
class LBDetail: Mappable {
var cardTypeID : Int?
var cardInfo: LBBaseCardInfo?
required init?(map: Map) {
}
func mapping(map: Map)
{
cardInfo <- map["cardInfo"]
}
}
class LBBaseCardInfo: Mappable {
var text: String?
var media: LBMedia?
required init?(map: Map) {
}
func mapping(map: Map) {
text <- map["text"]
media <- map["media"]
}
}
class CardType1: LBBaseCardInfo {
var background, userName : String?
required init?(map: Map) {
super.init(map: map)
}
override func mapping(map: Map) {
super.mapping(map: map)
background <- map["background"]
userName <- map["userName"]
}
}
class CardType2: LBBaseCardInfo {
required init?(map: Map) {
super.init(map: map)
}
override func mapping(map: Map) {
super.mapping(map: map)
}
}
class LBMedia: Mappable {
var mediaURL: String?
var mediaType: String?
var mediaThumbURL: String?
required init?(map: Map) {
}
func mapping(map: Map) {
mediaURL <- map["mediaUrl"]
mediaType <- map["mediaType"]
mediaThumbURL <- map["mediaThumbUrl"]
}
}
Please Kindly help me out to understand this framework.