Alamofire:发送参数

时间:2017-08-01 11:52:50

标签: ios swift3 alamofire

我想将数据发布为如下参数:

{ 
    "data":
    [
        {
            "nik" : "lalaal"
        }
    ]
}

如何使用Alamofire

在Swift 3中写入这些参数

我试过了:

let parameter: Parameters = [
                "data":[[
                    "nik" : self.nik,
                    "check_type" : "IN",
                    "tanggal" : "01-08-2017 18:22:00",
                    "long" : String(locationList[projectChoosen].long!),
                    "lat" : String(locationList[projectChoosen].lat!),
                    "id_loc" : locationList[projectChoosen].id_project,
                    "id_project" : nil,
                    "nama_project" : locationList[projectChoosen].nama_project,
                    "barcode" : "",
                    "foto": "",
                    "mime_type" : "image/jpeg"

                    ]]
            ]

4 个答案:

答案 0 :(得分:3)

您可以使用以下代码。

swap()

答案 1 :(得分:1)

您可以先在此格式中创建一个Dictionary对象

{ 
   "data":
   [
       {
           "nik" : "lalaal"
       }
   ]
}

之后,您可以使用NSJSONSerlisation将其转换为json字符串 而不是使用Almofire。

let array: [[String: String]] = [["nik": "lalaal"]]
let data =  try JSONSerialization.data(withJSONObject: array, options: JSONSerialization.WritingOptions.prettyPrinted)
let string = String(data: data, encoding: String.Encoding.utf8)
let postParam: [String: String] = ["data": string]

let _ = Alamofire.request(apiType.url!, method: apiType.type!,parameters: postParam, encoding: JSONEncoding.prettyPrinted, headers: nil).validate(statusCode: 200..<500).responseJSON { (response) in

}

答案 2 :(得分:0)

以下是使用Alamofire并将字典作为参数发布的示例请求:

let dataArray: [[String: String]] = [["nik": "lalaal"]]
let param: [String: [Any]] = ["data": dataArray]    
Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseJSON { response in }

答案 3 :(得分:0)

我使用:

let dataIn: [String: String] = ["param1": "value1"]
let paramGo: [String: [String: String]] = ["data_title": dataIn]    
Alamofire.request(url, method: .post, parameters: paramGo, encoding: JSONEncoding.default, headers: nil).responseJSON { response in }