通过Alamofire发送json数组

编程入门 行业动态 更新时间:2024-10-23 12:31:29
本文介绍了通过Alamofire发送json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否可以在POST请求中直接发送数组(未包装在字典中)。显然,参数参数应该获得以下内容的映射:[String:AnyObject]? 但我希望能够发送以下示例json:

I wonder if it's possible to directly send an array (not wrapped in a dictionary) in a POST request. Apparently the parameters parameter should get a map of: [String: AnyObject]? But I want to be able to send the following example json:

[ "06786984572365", "06644857247565", "06649998782227" ]

推荐答案

您只需使用 NSJSONSerialization 编码JSON,然后自己构建 NSURLRequest 。例如,在Swift 3中:

You can just encode the JSON with NSJSONSerialization and then build the NSURLRequest yourself. For example, in Swift 3:

var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") let values = ["06786984572365", "06644857247565", "06649998782227"] request.httpBody = try! JSONSerialization.data(withJSONObject: values) Alamofire.request(request) .responseJSON { response in // do whatever you want here switch response.result { case .failure(let error): print(error) if let data = response.data, let responseString = String(data: data, encoding: .utf8) { print(responseString) } case .success(let responseObject): print(responseObject) } }

对于Swift 2,请参见上一个此答案的修订版本。

For Swift 2, see previous revision of this answer.

更多推荐

通过Alamofire发送json数组

本文发布于:2023-11-26 03:08:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Alamofire   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!