Map a JSON with different data using ObjectMapper

181 views Asked by At

I have a JSON with the same key but the data is different. I would like to map that data creating different classes of objects depending in the type of the data, using ObjectMapper in swift. It's posible?

I have this JSON:

{
 "info":[
    {
        "type": "listing",
        "content_type": "basic_data",
        "data": [
            {
                "text": "Year",
                "value": "1972"
            }
        ]
    },
    {
        "type": "map",
        "content_type": "location",
        "data": [
            {
                "latitude": 43.5423,
                "longitude": -93.223,
                "zoom": 9
            }
        ]
    }
        ]
}

And i create this classes

class Info : NSObject, Mappable {

    var info : [Container]?;

    override init() {
        super.init();
    }

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        info         <- map["info"]
    }
}

class Container : NSObject, Mappable {

    var type : String?;
    var content_type : String?;
    var data : AnyObject?;


    override init() {
        super.init();
    }

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        type                <- map["type"]
        content_type        <- map["content_type"]

    }
}

I would like to map the key "data" to an object Location and to an object BasicData depending in the type. How can i do this?

Thanks

0

There are 0 answers