【发布时间】:2023-04-05 05:31:01
【问题描述】:
如何在 swift 中使用 alamofire 发布 json 对象数组?
我的最终数据(我想发布)看起来像:
temp = [{
"time": 1,
"score": 20,
"status": true,
"answer": 456
},
{
"time": 0,
"score": 0,
"status": false,
"answer": 234
},
{
"time": 0,
"score": 20,
"status": true,
"answer": 123
}
]
我得到提示,我必须创建自定义参数编码,但我很困惑我该怎么做。有人请帮帮我。
my current code looks like
let parameters: Parameters = [
"answers": temp,
"challenge_date": "2019-03-01"
]
Alamofire.request("...url", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON {
response in
if
let status = response.response ? .statusCode {
let classFinal: JSON = JSON(response.result.value!)
if (status > 199 && status < 300) {
self.dismiss(animated: true)
} else {
}
}
}
【问题讨论】:
-
我猜 temp 是你的数组。您遇到的任何错误?
-
您需要以array & dict的格式发送temp。仔细看,将
[ ... ]视为数组,将{ ... }视为字典。现在用这个设置 answers 值。 注意 您的 post req 参数数据始终在数组中,而不是 json。 -
@GaneshSomani “JSON 解析错误 - 无法解码 JSON 对象”
-
您的
Parameters类是如何实现的。这就是你的错误的根源。 -
请查看已编辑的问题。这是我的最后一个案例。
标签: ios arrays json swift alamofire