1
votes

I am using the below link to map the objects.

https://github.com/Hearst-DD/ObjectMapper

I am facing the issue while mapping the serviceids (Json structure in below) array object which is without key.

Kindly help me on the if someone worked on it.

Regards, Guddu.

Json Structure:

Consumer = (
   {
address = "12120 Sunset Hills Road Reston Virginia";
                clientid = 280396;
                consumerid = 280396;
                lag = 0;
                lat = 0;
                name = "William Holland";
                zipcode = 64321;
serviceids =                 (
                    5,
                    100,
                    101
    )
}

In Consumer mapper class:

serviceIdsArray <- map["serviceids"]
        serviceids = NSSet(array: serviceIdsArray!)

In Serviceids class :

func mapping(map: Map) {

        serviceid <- map[?]

    }
1

1 Answers

0
votes

the model should be like this :

class JsonModel: Mappable {
var address: String?
var clientid: Int?
var consumerid: Int?
var lag: Int?
var lat: Int?
var name: String?
var zipcode: Int?
var serviceids: [Int]?
required init?(_ map: Map) {

}

// Mappable
func mapping(map: Map) {
    address    <- map["address"]
    clientid    <- map["clientid"]
    consumerid    <- map["consumerid"]
    lag    <- map["lag"]
    lat    <- map["lat"]
    name    <- map["name"]
    zipcode    <- map["zipcode"]
    serviceids    <- map["serviceids"]
}
}

then use :

let json =   [
        "address" : "12120 Sunset Hills Road Reston Virginia",
        "clientid" : 280396,
        "consumerid" : 280396,
        "lag" : 0,
        "lat" : 0,
        "name" : "William Holland",
        "zipcode" : 64321,
        "serviceids" :[
            5,
            100,
            101
        ]
    ]
    let model = JsonModel(JSON: son)

serviceids just an array,don't need to make a model for it