无法使用对象映射器解析双字符串下方

Unable to parse underneath double string with Object Mapper

我有一个包含这两个字段的模型

var latitude: String?
var longitude: String?

我正在尝试解析模型。一切都被正确解析,但只有这两个字段从未被解析。

这是完整的模型

import ObjectMapper
class BarberLoginModel: Mappable {
var id: Int?
var barberShopId: Int = 0
var firstName = ""
var lastName = ""
var mobileNo = ""
var userName = ""
var email = ""
var imgPath = ""
var shopName = ""
var address = ""
var street = ""
var area = ""
var emirates = ""
var latitude: String?
var longitude: String?
var Services: [ServiceModel]?
var Stylist: StylistArrayModel?
var Availability: [AvailabilityModel]?
var Facilities: [FacilitiesModel]?

required init?(map: Map) {

}

func mapping(map: Map) {
    id <- map["id"]
    barberShopId <- map["barberShopId"]
    firstName <- map["firstName"]
    lastName <- map["lastName"]
    mobileNo <- map["mobileNo"]
    userName <- map["userName"]
    email <- map["email"]
    imgPath <- map["imgPath"]
    shopName <- map["shopName"]
    address <- map["address"]
    street <- map["street"]
    area <- map["area"]
    emirates <- map["emirates"]
    latitude <- map["latitude"]
    longitude <- map["longitude"]
    Services <- map["Services"]
    Stylist <- map["Stylist"]
    Availability <- map["Availability"]
    Facilities <- map["Facilities"]
}
}

注意:当我将数据类型更改为双精度时?它可以工作,但它会添加额外的数字,因为它是双倍的。

Link 到 json: http://shaveme.stagingserver-me.com/api/ShopUserMaster?ln=en&t=1F10D5778C66A751766E8532EEAA0E1B&uid=21

在无法访问实际数据的情况下很难进行调试。我在下面进行了一些测试。如果你 运行 它,你会知道第二个 XCTAssertEqual 在每个测试中都失败了,因为原始 JSON 数据在值 26.432 周围没有引号。我的猜测是你的数据是一个数字而不是一个字符串,因此不会被正确解组。

import XCTest
import ObjectMapper

class Test : Mappable {
    var latitude: String?
    var longitude: String?

    required init?(map: Map) {

    }

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

class ObjectMapper_RndTests: XCTestCase {

    override func setUp() {
        super.setUp()
    }

    override func tearDown() {
        super.tearDown()
    }

    func testExample() {
        let json = "{\"longitude\": 26.432, \"latitude\":\"82.123\"}"
        let test = Test(JSONString: json);

        guard let t = test else {
            return
        }

        XCTAssertEqual(t.latitude, "82.123")
        XCTAssertEqual(t.longitude, "26.432")
    }

    func testExampleTwo() {
        let json = [ "longitude": 26.432, "latitude": "82.123" ] as [String : Any]
        let test = Test(JSON: json);

        guard let t = test else {
            return
        }

        XCTAssertEqual(t.latitude, "82.123")
        XCTAssertEqual(t.longitude, "26.432")
    }
}

如果无法将数据源更改为字符串,解决方案是使用转换。在 ObjectMapper 站点 https://github.com/Hearst-DD/ObjectMapper 上有更多信息。看起来像这样。

class Test : Mappable {
    var latitude: String?
    var longitude: String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        let transform = TransformOf<String, Double>(fromJSON: { (value: Double?) -> String? in

            if let v = value {
                NSLog("from json value: " + String(v))
                return String(v)
            }
            NSLog("from json value: null")
            return nil
        }, toJSON: { (value: String?) -> Double? in
            // transform value from Double? to String?
            if let value = value {
                return Double(value)
            }
            return nil
        })

        latitude <- (map["latitude"], transform)
        longitude <- (map["longitude"], transform)
    }
}

或者,您可以手动完成。更简洁、更短的代码

class BarberLoginModel: Mappable {

    var latitude: Double?
    var longitude: Double?

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        var latitude: String?
        var  longitude: String?
        latitude <- map["latitude"]
        longitude <- map["longitude"]

        if let latitude = latitude {
            self.latitude = Double(string: latitude)
        }
        if let longitude = longitude {
            self.longitude = Double(string: longitude)
        }
    }
}