【问题标题】:Post A Json object as parameter value in Swift 4 ios using Alamofire使用 Alamofire 在 Swift 4 ios 中发布一个 Json 对象作为参数值
【发布时间】:2019-01-14 05:25:48
【问题描述】:

我尝试了很多方法,但每次都失败了。 我想将 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;

})

我尝试了很多方法,都没有为我解决。请给我您宝贵的建议。

谢谢

【问题讨论】:

  • 你遇到了什么错误?
  • 我在邮递员中发布了相同的参数,它给出的响应为'状态:“成功”'
  • 但是通过上面的代码,得到状态错误
  • 请在您的问题中添加您的错误。
  • 没有错误,就是没有响应成功

标签: swift post alamofire


【解决方案1】:

**使用 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对象。

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多