sahiljain
sahiljain

Reputation: 2374

How to use objectmapper for nested json in swift?

I am using object mapper library in swift fro parsing JSON to object and I have a JSON which looks like this:

"_links" :     {
    "category.genres":         {
        "href" : "http://dev.abcd.com/api/v1/categories/series/genres"
    }

}

I am trying to map href to model but I am not able to find any solution here. I tried to do

genreLink <- (map["_links"]["category.genres",nested: false]["genres"],urlTransform)

But it doesn't work, Please guide. Thanks

Upvotes: 0

Views: 1302

Answers (2)

Rayan
Rayan

Reputation: 1

As per documentation:

When you have nested keys which contain ., you can pass the custom nested key delimiter as follows (#629):

func mapping(map: Map) {
    appName <- map["com.myapp.info->com.myapp.name", delimiter: "->"]
}

Reference: Custom nested key delimiter in mapping

Upvotes: 0

tristan_him
tristan_him

Reputation: 276

Unfortunately, I don't think your situation is supported using the nested keys feature of ObjectMapper. This is because of one keys in the nesting already has a period in it. If it didn't the mapping would look like the following:

genreLink <- (map["_links.category_genre.href"], urlTransform)

Chaining the mappings as you have done above is not supported. To properly map this, I think you will need to create some model classes (that implement Mappable) for Links and Category.genres or change the API response if possible.

Upvotes: 3

Related Questions