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

[ad_1]

I’ve a JSON response (bellow) and I have to 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 can’t in a position to parse the teams as that’s not a listing however an object –

    import ObjectMapper
    
    class Particular person: BaseObject {
        @objc dynamic var ID: Int = -1
        @objc dynamic var identify: String = ""
        @objc dynamic var age: Int = -1
        
        var friendsList = Checklist<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 unhealthy JSON. The teams needs to be on the listing 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 be dynamic. Thus I can’t in a position to parse this as associates attribute.

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

[ad_2]

Leave a Reply