在我的应用程序中我使用AlamofireObjectMapper进行映射。我第一次使用这个。这是我从API
获得的回复 {
Message = "email verification link has been sent to your email. please verify your account.";
Result = {
"V002_vendors_type" = "<null>";
"V003_pharmacy" = ();
"V010_subscription" = "<null>";
"attempt_date" = "<null>";
created = "2016-04-26T11:07:30.3192745+00:00";
email = "abc@gmail.com";
"first_name" = abc;
id = 10167;
"is_lock" = 0;
"last_name" = "<null>";
mobile = 9999999999;
password = xxxxxxxxx;
"profile_Image" = "<null>";
status = PV;
subscription = 1;
updated = "2016-04-26T11:07:30.3192745+00:00";
"Fix_id" = 1;
};
Status = 1;
}
现在这是我的代码
func pharmacySignUp()
{
let url = "http://\(basicURL)vendor_signup"
let param :[String : AnyObject] =
[
"email" : txtemail.text!,
"password" : txtpassword.text!,
"mobile" : txtmobile.text!,
"first_name" : txtname.text!
]
Alamofire.request(.POST, url, parameters: param, encoding: .JSON).responseObject { (response:Response<signupVarificationCode, NSError>) in
print(response.result.value)
let signupVarificationCode = response.result.value
print(signupVarificationCode)
print(signupVarificationCode!.Message)
print(signupVarificationCode?.status)
}
这是我用于映射的类
class signupVarificationCode: Mappable {
var Message : String?
var status : String?
required init?(_ map: Map){
}
func mapping(map: Map) {
Message <- map["Message"]
status <- map["Status"]
}
}
通过这段代码,我可以获得消息,但现在我想映射Result对象,那么我该怎么做呢?
感谢Yuvraj Sinh工作,但我想从Result对象访问所有变量,所以我创建了这个对象
class Result: Mappable {
var lastName : String?
var mobile : String?
var id: String?
required init?(_ map: Map){
}
func mapping(map: Map) {
lastName <- map["last_name"]
mobile <- map["mobile"]
id <- map["id"]
}
}
我想在pharmacySignup方法中打印移动值。那怎么能这样呢?
答案 0 :(得分:1)
由于API响应中的Result参数表示另一个JSON对象,因此应使用Dictionary进行映射。您可以使用以下内容替换当前的signupVarificationCode。
class signupVarificationCode: Mappable {
var Message : String?
var status : String?
var result : [String:AnyObject]?
required init?(_ map: Map){
}
func mapping(map: Map) {
Message <- map["Message"]
status <- map["Status"]
result <- map["Result"] as! [String:AnyObject]
}
}
如果你想更加面向对象,那么你可以为Result
创建单独的类,并且可以以相同的方式使用。
答案 1 :(得分:0)
var result:[String:AnyObject] = map[“Result”] as! [String:AnyObject]
//在这里你得到结果中的字典 通过使用此dict,您可以获得任何键值对 //它只是一个正常的解析希望它会起作用
答案 2 :(得分:0)
你必须只相信其他名为Result
的可映射类 class signupVarificationCode: Mappable {
var Message : String?
var status : String?
var result : Result?
required init?(_ map: Map){
}
func mapping(map: Map) {
Message <- map["Message"]
status <- map["Status"]
result <- map["Result"]
}
}
class Result: Mappable {
var mobile: String?
required init?(_ map: Map){
}
func mapping(map: Map) {
//Mapping attributtes for example
mobile <- map["mobile"]
}
}