我的JSON是:
[
{
"a": "1",
"b": "2"
}
]
let parameters: Parameters = [
<here goes the JSON>
]
Alamofire.request(URL, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate().responseJSON
我知道参数是[String:AnyObject]的字典,但我需要像我的第一个JSON一样传递它
我需要发送它作为参数,但我想我只能发送一个[String:AnyObject],或者至少那个例子如何在Alamofire迁移指南中工作,所以我的问题是如何实现这一点? 我正在使用Alamofire 4
答案 0 :(得分:0)
我使用了一个名为SwiftyJSON的库,您只需这样做:
var jsonArray: JSON = [
"array": [1, 2],
"users": [
[
"id": 1,
"info": [
"name": "name1",
"email": "email1"
],
"nums": [123, 124, 125]
],
[
"id": 2,
"info": [
"name": "name2",
"email": "email2"
],
"nums": [12, 13, 14]
]
]]
答案 1 :(得分:0)
var request = URLRequest(url: URL(string: yourURL)!)
request.httpBody = try! JSON(yourObject).rawData()
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).validate(statusCode: 200..<600).responseJSON { response in
print("Response \(response)")
}
JSON是一个lib:SwiftJSON