![](/img/trans.png)
[英]How to post an array of dictionary (json object), in swift using alamofire?
[英]post JSON object with a dictionary and an array of dictionaries using alamofire
我正在尝试发布这样的对象:
{
"simulation" : { },
"simOptions" : [
{ },
{ }
]
}
并尝试通过调用SimulationRepository类将其发布:
SimulationsRepository().confirmSimulation(params: parameters) { (response) in
if let error = checkError(response) {
self.hideLoading()
self.showAlert(error)
return
}
guard let simsArray = SimulacaoArray(responseObject: response.result.value) else {
let error = response.error.debugDescription
self.hideLoading()
self.showAlert(error)
return
}
print(simsArray.simulation.count)
}
我在这里尝试了通过Alamofire发送json数组的方法 ,在Alamofire: 通过 字典数组发送JSON的方法 ,但是无法正确地转换字典数组。
好的,我已经解决了,方法是映射模拟商品数组并将其转换为字典,如下所示:
class SimulationsRepository: BaseRepository {
init() {
super.init(url: "/Simulations")
super.BaseUrl = "http://0.0.0.0:3000/api"
}
func confirmSimulation(simulation: Simulation, goods: [SimulatedGoods], then: @escaping ThenBlock) {
let goodsDict = goods.map { (simulatedGoods) -> [String: Any] in
return simulatedGoods.toDictionary()
}
let sim = simulation.toDictionary()
let params: [String: Any] = [
"simulation": sim,
"goods": goodsDict
]
super.customPost("/confirm", parameters: params, then: then)
}
}
顺便说一句,我从书中得到了映射这样的数组的想法: https : //www.hackingwithswift.com/store/pro-swift
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.