1

我正在使用下面的链接来映射对象。

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

我在映射没有键的serviceidsJson下面的结构)数组对象时遇到了这个问题。

如果有人在这方面工作,请帮助我。

问候,古杜。

json结构:

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
    )
}

在消费者映射器类中:

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

在 Serviceids 类中:

func mapping(map: Map) {

        serviceid <- map[?]

    }
4

1 回答 1

0

模型应该是这样的:

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"]
}
}

然后使用:

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 只是一个数组,不需要为它制作模型

于 2016-05-24T12:50:31.027 回答