Joc*_*her 6 json dictionary swift alamofire swift3
{"title":"exampleTitle","hashTags":[{"name":"tag1"},{"name":"tag2"}],"uploadFiles":
[{"fileBytes":"seriesOfBytes\n","filename":"upload.txt"}]}
Run Code Online (Sandbox Code Playgroud)
这是我想要发送到后端的我想要的身体.
我正在使用Swift 3.0和Alamofire 4,我有很多问题.
首先,我如何正确创建包含值和值数组的主体?
我的方法是:
let para:NSMutableDictionary = NSMutableDictionary()
para.setValue("exampleTitle", forKey: "title")
let jsonData = try! JSONSerialization.data(withJSONObject: para, options: .init(rawValue: 0))
let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as! String
print(jsonString)
Run Code Online (Sandbox Code Playgroud)
这给了我
{"title":"exampleTitle"}
Run Code Online (Sandbox Code Playgroud)
第二,我的alamofire .post请求如下所示,并且不起作用:
Alamofire.request(postURL, method: .post, parameters: jsonString, encoding: JSONEncoding.default)
.responseJSON { response in
debugPrint(response)
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:调用中的额外参数'方法'.如果我而不是jsonString使用该类型的字符串
var jsonString: [String : Any]
Run Code Online (Sandbox Code Playgroud)
它确实有效,但我不知道如何将身体放入这种类型.
总结 寻找帮助(例如将是最好的)如何创建身体,以及如何通过Alamofire 4和swift 3发送到我的后端.
Nir*_*v D 19
您需要将参数作为[String:Any]
字典传递,因此创建一个字典作为您传递的JSON.
let params = [
"title":"exampleTitle",
"hashTags": [["name":"tag1"],["name":"tag2"]],
"uploadFiles":[["fileBytes":"seriesOfBytes\n","filename":"upload.txt"]]
]
Run Code Online (Sandbox Code Playgroud)
现在将此params
作为参数传递给Alamofire请求.
Alamofire.request(postURL, method: .post, parameters: params, encoding: JSONEncoding.default)
.responseJSON { response in
debugPrint(response)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11166 次 |
最近记录: |