我使用ObjectMapper将我的JSON映射到Swift对象。如何使用ObjectMapper映射不同的类型?
我有以下夫特对象:
class User: Mappable {
var name: String?
var val: Int?
required init?(map: Map) { }
func mapping(map: Map) {
name <- map["name"]
val <- map["userId"]
}
}
我有这样的JSON结构:
{
"name": "first",
"userId": "1" // here is `String` type.
},
{
"name": "second",
"userId": 1 // here is `Int` type.
}
映射JSON,的User
的userId
其name
是"first"
为空后。
如何将Int/String
映射到Int
?
你不能这样做。你必须为UserId选择一个数据类型,不管它是Int还是String。 –
''val“'在哪里? userId在JSON中的关键是“userId”,对吧? –
@AnhTrungPham哦,这是我的错。是的,JSON中userId的关键是“userId”,我已经修复了它。谢谢。 –