【问题标题】:How to map Response Object to Alamofire 4.0 Post method如何将响应对象映射到 Alamofire 4.0 Post 方法
【发布时间】:2017-12-16 13:01:37
【问题描述】:

嗨,我是 swift 编程语言的新手,我想使用 Alamofire 4.0 POST 方法映射响应对象,为此我写了下面的代码,但它显示异常,如 ObjectMapiing failed 可以帮助我吗

视图控制器:-

let params = ["name": "admin",
                      "quantity": ""]

        Alamofire.request("Url",method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseObject {
            (response:DataResponse<Login>) in

            switch(response.result) {

            case .success(_):
                if response.result.value != nil{
                    print(response.result.value!)
                }
                break

            case .failure(_):
                print(response.result.error!)
                break
            }
        }

登录:-

 import UIKit
    import ObjectMapper

    class Login : Mappable{

       var CareInsYH:String?
    var CategoryYH:String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        CareInsYH <- map["CareInsYH"]
        CategoryYH <- map["CategoryYH"]
    }

   }

Json 响应:-

[
{
        CareInsYH = "";
        CategoryYH = "Type Stock GG - Gordyn        ";
        CollectionCodeYH = 0;
        CollectionsYH = "";
        ColourYH = "";
        CompositionYH = " ";
        ContinuityYH = "";
        CutQuantity = 0;
        DesignYH = "";
        ETAQuantity = 0;
        ExtraId = 14738;
        FRTestYH = "";
        HSCodeYH = "/img/Wash Care/Noimage.jpg";
        IDRPrice = 0;
        LightYH = 0;
        Location1YH = 0;
        Location2YH = 0;
        MartindaleYH = 0;
        MasterId = 14738;
        Name = "SAPPHIRE PTB 8902-1";
        POQuantity = 0;
        PillingYH = " ";
        ProductCode = "GG00787-01C";
        QtyOnHand = "97.72";
        RepeatYH = 0;
        ReservedQuantity = 0;
        RubbingYH = " ";
        SellingPrice = 625000;
        StatusYH = Stock;
        SupColourYH = "";
        SupDesignYH = "";
        SupplierCodeYH = "<null>";
        SupplierNameYH = 0;
        TotalAvlQuantity = "97.72";
        TypeYH = 0;
        USDPrice = 0;
        UsageYH = "";
        WashingYH = "";
        WeightYH = 0;
        WidthYH = "               ";
        WyzenbeekYH = " ";
        imageYH = "/img/products/SGG00787-01C.jpg";
}
]

【问题讨论】:

标签: swift alamofire


【解决方案1】:

JSON 响应不正确,正确示例:

[ 
{
        "CareInsYH": "Pain Management",
        "CutQuantity": 0
    }
]

将你的回复复制到JsonBlob,这是错误的。

【讨论】:

猜你喜欢
  • 2017-11-03
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2020-08-08
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多