gpt4 book ai didi

arrays - 在 swift 中使用 alamofire 发送 JSON 数组作为参数

转载 作者:行者123 更新时间:2023-11-28 15:25:59 27 4
gpt4 key购买 nike

我正在尝试使用 Alamofire 将此数据(JSON 格式)作为参数发送到 swift 3:

[ {"type":"confirm",
"refKey":"123456789",
"quantity": "1"} ]

但我无法将其转换为参数数据,因为接受的数据类型是[String:Any]

如何传递所需的参数?

最佳答案

我在这篇文章中找到了我的答案: Send an array as a parameter in a Alamofire POST request

使用Alamofire的JSONSerialization和URLRequest发送HTTPBody中的数据。

    //creates the request        

var request = URLRequest(url: try! "https://api.website.com/request".asURL())

//some header examples

request.httpMethod = "POST"
request.setValue("Bearer ACCESS_TOKEN_HERE",
forHTTPHeaderField: "Authorization")

request.setValue("application/json", forHTTPHeaderField: "Accept")

//parameter array

let values = ["value1", "value2", "value3"]

request.httpBody = try! JSONSerialization.data(withJSONObject: values)

//now just use the request with Alamofire

Alamofire.request(request).responseJSON { response in

switch (response.result) {
case .success:

//success code here

case .failure(let error):

//failure code here
}
}

作者:mourodrigo

关于arrays - 在 swift 中使用 alamofire 发送 JSON 数组作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com