Use ObjectMapper to parse Dictionary within a Dictionary Swift

1.4k views Asked by At

My old JSON structure was like this:

{
"parameters": {
"customerId": 9,
"from": "2014-06-05T14:00:00",
"until": "2014-06-05T15:00:00",
"km": 20,
"insurance": false
},
"estimatesPerCategory": {
  "2": {
  "timeCost": 5,
  "kmCost": 6,
  ... 
 }

To parse JSON I use ObjectMapper. This code works very well:

if let myObject = Mapper<CostEstimateResult>().map(JSONObject: JSON) {

}

The class CostEstimateResult looks like this.

class CostEstimateResult : NSObject, Mappable {

var parameters:CostEstimateParameters?
var estimatesPerCategory:[String: CostEstimate]? // CostEstimate.id -> CostEstimate


override init() {}

required convenience init?(map: Map) {
    self.init()
    self.mapping(map: map)
}

 func mapping(map: Map) {
    estimatesPerCategory <- map["estimatesPerCategory"]
    parameters <- map["parameters"]
 }
}

And last but not least my CostEstimate class for estimatesPerCategory

class CostEstimate : NSObject, Mappable {

var timeCost:NSNumber?
...
override init() {}

required convenience init?(map: Map) {
    self.init()
    self.mapping(map: map)
}

func mapping(map: Map) {
    timeCost <- map["timeCost"]
    ...
    }
}

This is working. The mapper in CostEstimateResult will call the mapper in CostEstimates and I get my parsed data.

Now I changed the JSON Structure:

{
"parameters": {
//same like before
},
"estimatesPerCategory": {
 "2": {
  "total": {
    "preTax": {
      "value": 1,
      "text": "1,00 €"
    },
    "configTax": false
  }, 
 "3": {
     //same...
   }
 }
}

In my opinion in the basic structure nothing changes. estimatesPerCategory is still the same dictionary. The data behind estimatesPerCategory are changed, okay.

The problem is, when I call the mapper ...Mapper<CostEstimateResult>().map... the parameters will be parse, but not the costEstimates. The func mapping in CostEstimate isn't call anymore.

Long story short: It seems that the type of estimatesPerCategory in CostEstimateResult changes with the json structure. I can't figure out which type I need.

1

There are 1 answers

5
Bista On

Now with new JSON Structure, your Class CostEstimate structure will be changed and a new Class PreTax will be required to be created:

class CostEstimateResult : NSObject, Mappable {

    var parameters:CostEstimateParameters?
    var estimatesPerCategory:[String: CostEstimate]? // CostEstimate.id -> CostEstimate

    override init() {}

    required convenience init?(map: Map) {
        self.init()
        self.mapping(map: map)
    }

    func mapping(map: Map) {
        estimatesPerCategory <- map["estimatesPerCategory"]
    }
}


class CostEstimate : NSObject, Mappable {
    var total:totalTax?

    override init() {}

    required convenience init?(map: Map) {
        self.init()
        self.mapping(map: map)
    }

    func mapping(map: Map) {
        total <- map["total"]
    }
}

class totalTax : NSObject, Mappable {
    var preTax:PreTax?
    var configTax:Bool?

    override init() {}

    required convenience init?(map: Map) {
        self.init()
        self.mapping(map: map)
    }

    func mapping(map: Map) {
        preTax <- map["preTax"]
        configTax <- map["configTax"]
    }
}

class PreTax : NSObject, Mappable {

    var value:NSNumber?
    var text:String?

    override init() {}

    required convenience init?(map: Map) {
        self.init()
        self.mapping(map: map)
    }

    func mapping(map: Map) {
        value <- map["value"]
        text <- map["text"]
    }
}