Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift:Alamofire POST请求,以JSON对象作为主体_Json_Swift_Post_Alamofire - Fatal编程技术网

Swift:Alamofire POST请求,以JSON对象作为主体

Swift:Alamofire POST请求,以JSON对象作为主体,json,swift,post,alamofire,Json,Swift,Post,Alamofire,我有此要求: curl -X "POST" "myURL" \ -H 'Content-Type: application/json' \ -H 'User-Agent: myAgent)' \ -H 'Accept-Language: it-IT' \ -d $'{ "Summary": { "Discount": 1.099, "TotalAmount": 9.891, "SubtotalAmount": 10.99 }, "Token": "token", "InstantDiscount

我有此要求:

curl -X "POST" "myURL" \
-H 'Content-Type: application/json' \
-H 'User-Agent: myAgent)' \
-H 'Accept-Language: it-IT' \
-d $'{ "Summary": { "Discount": 1.099, "TotalAmount": 9.891, "SubtotalAmount": 10.99 }, "Token": "token", "InstantDiscountId": "id" }'

但我不知道如何使用JSON对象设置主体。对于Alamofire,我总是在请求中使用[String:Any]作为参数,但事实并非如此。

答案是只需将编码设置为
JSONEncoding。默认值

下面是它作为代码的样子

let request = Alamofire.request(
    myUrl,
    method: .post,
    parameters: [
        "Summary": [
            "Discount": 1.099,
            "TotalAmount": 9.891,
            "SubtotalAmount": 10.99
        ],
        "Token": "token",
        "InstantDiscountId": "id"
    ],
    encoding: JSONEncoding.default,
    headers: [
        "Content-Type": "application/json",
        "User-Agent": "myAgent",
        "Accept-Language": "it-IT"
    ]
)

只需将请求的编码设置为
JSONEncoding.default
@ZonilyJame如何为该JSON创建[String:Any]对象?我已经试过了…好吧,这只是我如何创建参数对象的问题。。。谢谢;)