tristanhimmelman / ObjectMapper

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

Is there any way to parse dynamic unknown JSON keys in the same order like JSON provided? #1039

Open thihaaung6245 opened 5 years ago

thihaaung6245 commented 5 years ago

Your JSON dictionary:

{
        "roomType": {
                "SSS": {
                       "name": "Deluxe King"
                },
                "ABC": {
                       "name": "Executive King"
                },
                "SXW": {
                       "name": "Theme Suit"
                }
        }
}

Your model:

class RoomType: Mappable {
    var data: [String: Room]?

    required init?(map: Map) {

    }

    // Mappable
    func mapping(map: Map) {
        data   <- map["roomType"]
    }
}

class Room: Mappable {
    var name: String?

    required init?(map: Map) {

    }

    // Mappable
    func mapping(map: Map) {
        name   <- map["name"]
    }
}

What you did:

if let result = Mapper<RoomType>().map(JSONObject: json) {
       if let data = result.data {
              for (key, value) in data {
                     print(key)
              }
       }

}

What you expected:

What I expect is, I want to get the order exactly like json string

"SSS", "ABC", "SXW"

What you got:

"SXW", "ABC", "SSS"

What I get is dynamic order due to dictionary container. So, is there any way to parse unknown dynamic key with the server JSON response provided order? Like Android Map?

arsalanj commented 5 years ago

Since you are using data as dictionary, have you tried data.sorted.. method?