本文介绍了json使用alamofire快速发布嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Alamofire在API中发布嵌套的json对象,我的对象结构就像这样
i want to post nested json objects in API using Alamofire my objects structere is like this
["example" :
{
"fname":"john",
"lnamed":"Doe"
},{
"fname":"john",
"lname":"Doe"
},
.
.
.
]
我的问题是,当我制作数组时,它变得像["example": [] ["fname":"john","lname":"Doe"],["fname" :"john","lname":"Doe"] ] ]因此,由于阵列的原因,它们额外多了一个方括号.以下是我的代码
my problem is when i'm making array it becomes like ["example":[["fname":"john","lname":"Doe"],["fname":"john","lname":"Doe"]]]so their is one square bracket extra because of the array. below is my codes
var exampleObj = [String:AnyObject]()
var examplesArray = [exampleObj]
for example in examples
{
exampleObj = ["fname":example[fname] as AnyObject, "lname":example["lname"] as AnyObject]
examplesArray.append(exampleObj)
}
let parameters = ["example": examplesArray]
推荐答案
我发现问题出在Alamofire请求之后,我忘了添加编码参数,而解决方法是
after while i discovered my problem was with the Alamofire request i forgot to add the encoding parameter and the solution is
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.httpBody)
这篇关于json使用alamofire快速发布嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!