ios – How you can map variable variety of nested objects in object mapper swift

[ad_1]

I’ve a JSON response (bellow) and I must parse this –

[
{
    "id":123,
    "name":"Fahim Rahman",
    "age":25,
    "friends":[
        {
            "firstName": "Imtiaz",
            "lastName": "Khan",
            "avatar_url": null
        }
    ],
    "teams":{
        "xcet":{
            "identify":"xcek cert etsh tnhg",
            "createdDate":"2022-10-31T10:00:48Z"
        },
        "juyt":{
            "identify":"jfd uyt you to",
            "createdDate":"2021-09-13T10:00:00Z"
        },
        "some random key":{
            "identify": "some identify",
            "createdDate":"2026-03-27T10:00:00Z"
        }
    }
}
]

To parse this in my code I’ve created this mannequin. I cannot capable of parse the teams as that’s not a listing however an object –

    import ObjectMapper
    
    class Individual: BaseObject {
        @objc dynamic var ID: Int = -1
        @objc dynamic var identify: String = ""
        @objc dynamic var age: Int = -1
        
        var friendsList = Listing<Pals>()
     
        override func mapping(map: ObjectMapper.Map) {
            ID <- map["id"]
            identify <- map["name"]
            age <- map["age"]
            friendsList <- map["friends"]
        }
    }

    class Pals: BaseObject {
        @objc dynamic var firstName: String = ""
        @objc dynamic var lastName: String = ""
        @objc dynamic var avatarURL: String = ""
   
        override func mapping(map: ObjectMapper.Map) {
            firstName <- map["firstName"]
            lastName <- map["name"]
            age <- map["lastName"]
            avatarURL <- map["avatar_url"]
        }
    }    

I do know it is a dangerous JSON. The teams ought to be on the record as a substitute of the nested objects however sadly, I am getting this response.

Right here within the response of teams, the variety of nested objects is dynamic and the important thing of the nested object can also be dynamic. Thus I cannot capable of parse this as pals attribute.

So my query is, how can I map the “teams”?

[ad_2]

Leave a Reply