Maciej Grodzki
Maciej Grodzki

Reputation: 71

ObjectMapper - map JSON dictionary as nested object

I am trying to user ObjectMapper to consume JSON response. So far my response looks like this:

{
  "paramsStructure": [
    {
      "tiles": {
        "0": {
          "layout": {
            "column": 0,
            "colSpan": "1",
            "rowSpan": "1",
            "row": 0
          },
          "type": "2"
        },
        "1": {
          "layout": {
            "column": 1,
            "colSpan": "1",
            "rowSpan": "1",
            "row": 0
          },
          "type": "2"
        }
      },
      "title": "...",
      "rowCount": "4",
      "colCount": "2",
      "key": "...",
      "icon": "..."
    }
  ]
}

So far I have created StructuresObject for whole paramsStructure and nested collection of Single Structure object. Now I want to map tiles into TileStructure objects collection nested inside Structure object looking like this.

class SingleStructure : Mappable{

    var columns: Int = 0
    var title: String = ""
    var key: String = ""
    var icon: String = ""
    var tilesStructure : [Int: TileStructure]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        title <- map["title"]
        key <- map["key"]
        icon <- map["icon"]
        columns <- (map["colCount"], TransformOf<Int, String>(
            fromJSON: {item in return Int(item!)},
            toJSON: {_ in return "$0"}))


        //need here parsing of tilesStructure
     }
}

I mainly I need to map this JSON tiles dictionary into [Int: TileStructure] where key is dictionary key and TileStructure is mappable object containing "layout" and "type" attributes.

Thank you in advance for your help :)

EDIT!!!

I tried denis_lor approach but when I run parsing data from RxAlamofire I get following exception:

keyNotFound(CodingKeys(stringValue: "tiles", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"tiles\", intValue: nil) (\"tiles\").", underlyingError: nil))

This is how I call request

 return RxAlamofire.requestData(.get, GlobalSettings.GET_DEVICE_MAIN_STRUCTURE, parameters: parameters, headers: headers)
        .debug()

        .mapObject(type: ParamsStructure.self)

And thats my object mapper:

extension ObservableType {

public func mapObject<T: Codable>(type: T.Type) -> Observable<T> {
    return flatMap { data -> Observable<T> in
        let responseTuple = data as? (HTTPURLResponse, Data)

        guard let jsonData = responseTuple?.1 else {
            throw NSError(
                domain: "",
                code: -1,
                userInfo: [NSLocalizedDescriptionKey: "Could not decode object"]
            )
        }

        let decoder = JSONDecoder()

        let object = try decoder.decode(T.self, from: jsonData)

        return Observable.just(object)
    }
}

}

I think that the problem is maybe encoding and thats what creates those escape "\" what falls to keys missmatch.

Upvotes: 0

Views: 973

Answers (1)

denis_lor
denis_lor

Reputation: 6547

The key here to work with json structure where you have dynamic keys is by using a Dictionary like I did with [String:Tile].

You can try with the new Swift4's Codable:

import Foundation

public struct ResultParamsStructure: Codable {
    public var paramsStructure: [ParamsStructure] = []

    enum CodingKeys: String, CodingKey {
        case paramsStructure = "paramsStructure"
    }
}

public struct ParamsStructure: Codable {
    public var tiles: [String:Tile] = [:]
    public var title: String = ""
    public var rowCount: String = ""
    public var colCount: String = ""
    public var key: String = ""
    public var icon: String = ""

    enum CodingKeys: String, CodingKey {
        case tiles = "tiles"
        case title = "title"
        case rowCount = "rowCount"
        case colCount = "colCount"
        case key = "key"
        case icon = "icon"
    }
}

public struct Tile: Codable {
    public var layout: Layout?
    public var type: String = ""

    enum CodingKeys: String, CodingKey {
        case layout = "layout"
        case type = "type"
    }
}

public struct Layout: Codable {
    public var column: Int = 0
    public var colSpan: String = ""
    public var rowSpan: String = ""
    public var row: Int = 0

    enum CodingKeys: String, CodingKey {
        case column = "column"
        case colSpan = "colSpan"
        case rowSpan = "rowSpan"
        case row = "row"
    }
}

let jsonString = """
{
  "paramsStructure": [
    {
      "tiles": {
        "0": {
          "layout": {
            "column": 0,
            "colSpan": "1",
            "rowSpan": "1",
            "row": 0
          },
          "type": "2"
        },
        "1": {
          "layout": {
            "column": 1,
            "colSpan": "1",
            "rowSpan": "1",
            "row": 0
          },
          "type": "2"
        }
      },
      "title": "...",
      "rowCount": "4",
      "colCount": "2",
      "key": "...",
      "icon": "..."
    }
  ]
}
"""

let json = jsonString.data(using: .utf8)!

let resultParamsStructure = try? JSONDecoder().decode(ResultParamsStructure.self, from: json)

print(resultParamsStructure?.paramsStructure[0].tiles.keys)
print(resultParamsStructure?.paramsStructure[0].tiles["1"]?.layout?.colSpan)
//# Optional(Dictionary.Keys(["0", "1"]))
//# Optional("1")

You can try the above code here: http://online.swiftplayground.run/

Upvotes: 1

Related Questions