使用Alamofire在Swift 4 iOS中将Json对象发布为参数值

时间:2018-08-07 07:41:18

标签: swift post alamofire

我正在尝试许多方法,但是每次失败都可以。 我想发布json对象作为键值 关键是“数据” 关键值是

{"totalcost":"8500","delivery_type":"take_away","order_name":"","order_mobilenumber":"","order_city":"Chose Your Place","order_address":"","take":"  2 Hour ","":[{"parent_id":"145","subitem_id":"179","quentity":"1"}],"customer_id":"6"}

我的代码是

let parameters: [String: Any] = [
        "data":
            ["customer_id":"6",
            "totalcost":"8500",
            "delivery_type":"take_away",
            "order_name":"",
            "order_mobilenumber":"",
            "order_city":"Chose Your Place",
            "order_address":"",
            "take_away_time":" Next 2 Hour ",
            "":[
                [
                  "parent_id":"145",
                  "subitem_id":"179",
                  "quentity":"1"
                ]
            ]
        ]

    ]

Alamofire.request("http:/app_con",method:.post,parameters:parameters,encoding:JSONEncoding.default).responseJSON{response in
        let jsonResult = response.result.value
        print(jsonResult)

    }

我正在接受回应

{
message = "Please Try After Some Time";
status = error;

}

但是通过邮递员,我得到了回应

{
message = "successfully submitted";
status = successful;

})

我尝试了很多方法,对我来说什么都没有解决。请给我您的宝贵建议。

谢谢

1 个答案:

答案 0 :(得分:0)

**这是通过使用SwiftyJson解决的。我搜索了许多站点,但没有得到预期的答案。 我发布答案,因为它对某人有用

解决方案是

I tried to post key value in dictionary format instead of json format,That's why i did not get expected output. swiftjson is very to write json object.**

let emptyJson : JSON = JSON(["customer_id":"26",
                                 "totalcost":"8500",
                                 "delivery_type":"take_away",
                                 "order_name":"",
                                 "order_mobilenumber":"",
                                 "order_city":"Chose Your Place",
                                 "order_address":"",
                                 "take_away_time":" Next 2 Hour ",
                                 "":[
                                    [
                                        "parent_id":"145",
                                        "subitem_id":"179",
                                        "quentity":"1"
                                    ]
                                    ]
                               ])
let parameters: [String: Any] = [
        "data": emptyJson
    ]
Alamofire.request("http://_con",method:.post,parameters:parameters).responseJSON{response in
        let jsonResult = response.result.value
        print(jsonResult)

    }

而且Alamofire请求中无需使用json编码。我们只是发布json对象。