且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Alamofire发送json数组

更新时间:2023-01-16 22:26:11

您只需使用 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.