swift - 使用 Alamofire 在 Swift 4 ios 中将 Json 对象作为参数值发布

标签 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;

}

但是通过 postman 我得到了以下回复

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

})

我尝试了很多方法,但没有任何效果。请给我您宝贵的建议。

谢谢

最佳答案

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

关于swift - 使用 Alamofire 在 Swift 4 ios 中将 Json 对象作为参数值发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51721400/

相关文章:

c# - HTTP 客户端 Cookie c#

ios - 无法创建有效的 JSON 对象?

ios - 使用 ReactiveX 管理多个上传(在 iOS 上使用 Swift 和 Alamofire)

ios - 如何在 Swift 项目中使用 Obj-c 库/自定义接口(interface)

ios - 如何在 subview 中获得一个按钮以在 View Controller 中启用 rightbarbuttonitem

ios - 将 NSDictionary 附加到 [NSDictionary] 会产生意想不到的结果

ios - swift 中的循环进度

javascript - 如何自动提交帖子?

ios - 为什么 completionHandler block 没有响应?

ios - 向文件添加新的目标成员身份会导致 "No such module"和 "Command CompileSwiftSources failed with a nonzero exit code"