Swift - Mapping of Nested Objects (Objectmapper) Swift - Mapping of Nested Objects (Objectmapper) json json

Swift - Mapping of Nested Objects (Objectmapper)


I'm assuming you are getting no values at all, because I tried your code and got nothing. If you only need the contents of the data array from the json and, as you have it now, ObjectMapper is expecting a json with just an array of PostModelSpeedRunModels. Therefore, you need to add a keyPath to tell AlamofireObjectMapper it's starting point:

    Alamofire.request("http://www.speedrun.com/api/v1/games", method: .get)        .responseArray(keyPath: "data") { (response: DataResponse<[PostModelSpeedRunModel]>) in            ...    }

If you also need the info from the pagination node, then you'll need to create a new object that has a data and pagination properties, and change responseArray(keyPath: ...) to simply responseObject using the new root object in DataResponse.

Then I believe you only want runs's uri, so I recommend just having a String in your model for storing it, instead of an array of Links. Assuming that the array of links is unsorted and may change order in the future (if not you can access directly like map["links.1.uri"] and you are done), all links need to be parsed and then filtered. It can be done as follows:

struct PostModelSpeedRunModel {    var id              = ""    var international   = ""    var abbreviation    = ""    var runsLink        = ""    var uri             = ""}extension PostModelSpeedRunModel: Mappable {    init?(map: Map) {    }    mutating func mapping(map: Map) {        id              <- map["id"]        international   <- map["international"]        abbreviation    <- map["abbreviation"]        uri             <- map["logo"]        var links: [Link]?        links <- map["links"]        if let uri = links?.first(where: {$0.rel == "runs"})?.uri {            runsLink = uri        }    }}struct Link {    var rel = ""    var uri = ""}extension Link: Mappable {    init?(map: Map) {    }    mutating func mapping(map: Map) {        rel <- map["rel"]        uri <- map["uri"]    }}