json post nested objects in swift using alamofire

喜欢而已 提交于 2019-12-11 04:22:27

问题


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"
 },
.
.
.
]

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] 

回答1:


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)


来源:https://stackoverflow.com/questions/40332886/json-post-nested-objects-in-swift-using-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!