tristanhimmelman / AlamofireObjectMapper

An Alamofire extension which converts JSON response data into swift objects using ObjectMapper
MIT License
2.66k stars 474 forks source link

Object mapper fails to fill response object if JSON response key contains a dot "." #250

Closed KarthikaReddy closed 6 years ago

KarthikaReddy commented 6 years ago

Here is sample JSON [ { "TemplateName": "Test Template", "template.image.details": { "image.width": 1024, "image.height": 1024, }, } ] Here is how my Mapper function looks public func mapping(map: Map) { templateName <- map["TemplateName"] details <- map["template.image.details"] } I am getting details object as nil from the Alamofire Object mapper library. I think it is because my JSON response keys contain a "." ("template.image.details"). How can this be fixed?

gcharita commented 6 years ago

As ObjectMapper README.md states . is the default delimeter for mapping of nested objects.

Try disabling this feature by mapping the details property like this:

details <- map["template.image.details", nested: false]
KarthikaReddy commented 6 years ago

Thanks, that worked!!

How can I use this in Alamofire Request responseObject(keyPath)? Say If I want to directly get image.width attribute Alamofire.request(url).responseObject(keyPath: "template.image.details.image.width") { } I have tried below code as well but did not work. Alamofire.request(url).responseObject(keyPath: "template.image.details->image.width") { }

gcharita commented 6 years ago

@KarthikaReddy this is probably a question for stackoverflow But given the fact that you have . in your JSON, I think that this is not possible.