tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.15k stars 1.03k forks source link

Map JSON without keys #1009

Closed KompoD closed 6 years ago

KompoD commented 6 years ago

My JSON dictionary:

{  
   "type":"playlist",
   "lastUpdated":1537534023,
   "list":[  
      [  
         456239399,
         "first",
         false
      ],
      [
         321312312,
         "second",
         true
      ]
   ] 
}

I tried to do this via Codable, but it didn't work well. Do you have some ideas how to map this via ObjectMapper?

gcharita commented 6 years ago

@KompoD you should be able map your JSON to the following model class:

class Result: Mappable {
    var type: String?
    var lastUpdated: Int64?
    var list: [[Any]]?

    required init?(map: Map) { }

    func mapping(map: Map) {
        type <- map["type"]
        lastUpdated <- map["lastUpdated"]
        list <- map["list"]
    }
}

using map(JSONString:) function of Mapper class like:

let result = Mapper<Result>().map(JSONString: jsonString)
KompoD commented 6 years ago

@gcharita Thank you, it works!