How to map Response Object to Alamofire 4.0 Post method

时间:2017-07-12 08:08:15

标签: swift alamofire

Hi i am new for swift programing language and i want to map Response Object using Alamofire 4.0 POST method for this i wrote below code but it's showing Exception like ObjectMapiing failed can some on help me please

ViewController:-

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
            }
        }

Login:-

 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 Response:-

[
{
        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";
}
]

1 个答案:

答案 0 :(得分:0)

JSON response is not correct, correct sample:

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

Copy your response to JsonBlob, it's wrong.