swift – 通过Alamofire发送json数组

swift – 通过Alamofire发送json数组,第1张

概述我想知道是否可以在POST请求中直接发送数组(不包含在字典中)。显然参数参数应该得到一个映射:[String:AnyObject]? 但我想能够发送以下示例json: [ "06786984572365", "06644857247565", "06649998782227"] 你可以用NSJSONSerialization编码JSON,然后自己生成NSURLReques 我想知道是否可以在POST请求中直接发送数组(不包含在字典中)。显然参数参数应该得到一个映射:[String:AnyObject]?
但我想能够发送以下示例Json:
[    "06786984572365","06644857247565","06649998782227"]
你可以用NSJsONSerialization编码JsON,然后自己生成NSURLRequest。例如,在Swift 2:
let request = NSMutableURLRequest(URL: url)request.httpMethod = "POST"request.setValue("application/Json",forhttpheaderFIEld: "Content-Type")let values = ["06786984572365","06649998782227"]request.httpBody = try! NSJsONSerialization.dataWithJsONObject(values,options: [])Alamofire.request(request)    .responseJsON { response in        // do whatever you want here        switch response.result {        case .Failure(let error):            print(error)            // if web service reports error,sometimes the body of the response            // includes description of the nature of the problem,e.g.            if let data = response.data,let responseString = String(data: data,enCoding: NSUTF8StringEnCoding) {                print(responseString)            }        case .Success(let responSEObject):            print(responSEObject)        }}

或者,在Swift 3:

var request = URLRequest(url: url)request.httpMethod = "POST"request.setValue("application/Json","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,enCoding: .utf8) {                print(responseString)            }        case .success(let responSEObject):            print(responSEObject)        }}
总结

以上是内存溢出为你收集整理的swift – 通过Alamofire发送json数组全部内容,希望文章能够帮你解决swift – 通过Alamofire发送json数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存