ObjectMapper - nested dynamic keys

I am writing in Swift 3.1 using ObjectMapper to map my JSON response to my models.

I am trying to map this rather complex JSON response to dynamic keys, and I am hoping to get some feedback on what I am doing wrong.

The group has statistics about this progress. It has statistics broken down into years and then months. Every month for a year has results, ROI and winnings. ROI and winnings are only percentages, but the result key is captured using the keys below, 1-5, and then an integer as value.

My JSON

"stats": {
    "2017": {
        "1": {
            "results": {
                "1": 13,
                "2": 3,
                "3": 1,
                "4": 1,
                "5": 0
            },
            "roi": 0.40337966202464975,
            "win": 0.8181818181818182
        },
        "2": {
            "results": {
                "1": 13,
                "2": 5,
                "3": 1,
                "4": 2,
                "5": 1
            },
            "roi": 0.26852551067922953,
            "win": 0.717948717948718
        }
    }
}

      

My Models

class GroupResponse: Mappable {
    var stats: [String: [String: StatsMonthResponse]]?

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

class StatsMonthResponse: Mappable {
    var tips: [String: Int]?
    var roi: Double?
    var win: Double?

    func mapping(map: Map) {
        tips    <- map["results"]
        roi     <- map["roi"]
        win     <- map["win"]
    }
}

      

What i get

The answer I get has the stats property in my GroupResponse class as nil.

What other approach could I take to achieve this goal, or change my implementation to do this?

+3


source to share


1 answer


Decision

I solved the problem by mapping JSON manually.

class GroupResponse: Mappable {

    var stats: [String: StatsYear]?

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

class StatsYear: Mappable {

    var months: [String: StatsMonth] = [:]

    override func mapping(map: Map) {

        for (monthKey, monthValue) in map.JSON as! [String: [String: Any]] {

            let month = StatsMonth()

            for (monthKeyType, valueKeyType) in monthValue {

                if monthKeyType == "results" {
                    let tipResultDict = valueKeyType as! [String: Int]

                    for (result, tipsForResult) in tipResultDict {
                        month.tips[result] = tipsForResult
                    }
                }
                else if monthKeyType == "roi" {
                    month.roi = valueKeyType as? Double
                }
                else if monthKeyType == "win" {
                    month.win = valueKeyType as? Double
                }
            }
            months[monthKey] = month
        }
    }
}

class StatsMonth {

    var tips: [String: Int] = [:]
    var roi: Double?
    var win: Double?
}

      



This is probably the best solution to this problem, but for now this is what I am sticking with.

Hope this helps!

+1


source







All Articles