对象映射到根 Json return 无
Object Mapping to the Root Json return nil
我需要使用 AlamofireObjectMapper 映射以下 Json:
{
"user": {
"id": 2,
"first_name": "Dealer",
"last_name": "Seller",
"email": "seller@winfooz.com",
"authentication_token": "L6HzhZWdWhtxNwVkrsjY",
"documents_uploaded": false,
"type": "Dealer"
}
}
我写了以下部分代码,但是映射对象return nil!
class SystemUser:Mappable{
func mapping(map: Map) {
type <- map[SystemUserIdentifiers.UserType] //"type"
firstName <- map[SystemUserIdentifiers.FirstName]
lastName <- map[SystemUserIdentifiers.LastName]
internalIdentifier <- map[SystemUserIdentifiers.InternalIdentifier]
email <- map[SystemUserIdentifiers.Email]
documentsUploaded <- map[SystemUserIdentifiers.DocumentsUploaded]
authenticationToken <- map[SystemUserIdentifiers.AuthenticationToken]
}
}
这里正在发送 post 请求:
Alamofire.request(.POST, URL, parameters: parameters, encoding: .JSON).responseObject {
(response: Response<SystemUser, NSError>) in
guard let user = response.result.value else{
return
}
print(user.authenticationToken)
}
映射 root 的正确方法是什么 Json?
我想你必须制作两个可映射的对象;一个用于响应,另一个用于用户;喜欢
//Response
class ServerResponse:Mappable{
var user: SystemUser?
func mapping(map: Map) {
user <- map["user"]
}
}
-
//User class
class SystemUser:Mappable{
var type:String?
var firstName:String?
var lastName:String?
// ... Further properties
func mapping(map: Map) {
type <- map["type"] //"type"
firstName <- map["first_name"]
lastName <- map["last_name"]
// ... Further properties
}
}
现在
Alamofire.request(.POST, URL, parameters: parameters, encoding: .JSON).responseObject {
(response: Response<ServerResponse, NSError>) in
print(response.user?.authenticationToken)
//Do your work here
}
另一种选择是您要求后端开发人员删除该用户密钥并在单个字典中发送数据
我需要使用 AlamofireObjectMapper 映射以下 Json:
{
"user": {
"id": 2,
"first_name": "Dealer",
"last_name": "Seller",
"email": "seller@winfooz.com",
"authentication_token": "L6HzhZWdWhtxNwVkrsjY",
"documents_uploaded": false,
"type": "Dealer"
}
}
我写了以下部分代码,但是映射对象return nil!
class SystemUser:Mappable{
func mapping(map: Map) {
type <- map[SystemUserIdentifiers.UserType] //"type"
firstName <- map[SystemUserIdentifiers.FirstName]
lastName <- map[SystemUserIdentifiers.LastName]
internalIdentifier <- map[SystemUserIdentifiers.InternalIdentifier]
email <- map[SystemUserIdentifiers.Email]
documentsUploaded <- map[SystemUserIdentifiers.DocumentsUploaded]
authenticationToken <- map[SystemUserIdentifiers.AuthenticationToken]
}
}
这里正在发送 post 请求:
Alamofire.request(.POST, URL, parameters: parameters, encoding: .JSON).responseObject {
(response: Response<SystemUser, NSError>) in
guard let user = response.result.value else{
return
}
print(user.authenticationToken)
}
映射 root 的正确方法是什么 Json?
我想你必须制作两个可映射的对象;一个用于响应,另一个用于用户;喜欢
//Response
class ServerResponse:Mappable{
var user: SystemUser?
func mapping(map: Map) {
user <- map["user"]
}
}
-
//User class
class SystemUser:Mappable{
var type:String?
var firstName:String?
var lastName:String?
// ... Further properties
func mapping(map: Map) {
type <- map["type"] //"type"
firstName <- map["first_name"]
lastName <- map["last_name"]
// ... Further properties
}
}
现在
Alamofire.request(.POST, URL, parameters: parameters, encoding: .JSON).responseObject {
(response: Response<ServerResponse, NSError>) in
print(response.user?.authenticationToken)
//Do your work here
}
另一种选择是您要求后端开发人员删除该用户密钥并在单个字典中发送数据