JSON.dictionaryObject 在 Alamofire 中为空 swift 3

JSON.dictionaryObject is null in Alamofire swift 3

我有一个 JSON,格式如下:

postJson ={"firstname":"Vishal","lastname":"raskar","username":"vishal123","password":"123456","confirmpassword":"123456","email":"raskarvishal7@gmail.com","timezone":"1","accountid":"12345","phoneno":"8655012753"}

(postJson的数据类型是JSON即swiftyJSON)

现在我想通过Alamofire打服务器,所以我需要Post JSON字典格式的数据在parameters : ___,例如:

Alamofire.request(URL, method: .post, parameters: postJson.dictionaryObject, encoding: JSONEncoding.default,headers: "Content-Type": "application/json").responseJSON { response in switch response.result {

case .success(let data):

case .failure(let error):

}

所以基本上我尝试通过 postJson.dictionaryObject 转换字典中的 JSON。但总是从 postJson.dictionaryObject 中获取空值(即使数据存在于 postJson 中)。

我尝试了 postJson.dictionaryValuepostJson.dictionary 等所有组合,但没有成功。

然后我尝试将 postJson 转换为 Data,然后是字典:

if let data = postJson.data(using: .utf8) {

 do {

return try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
            } 
catch {

 print(error.localizedDescription)

 }

 }

然后 post 通过 Alamofire,现在得到响应。我究竟做错了什么?我想使用我的第一个选项。

假设您使用的是 Swift 3 / Alamofire 4.0

这是根据 Alamofire 文档定义 "parameters" 参数的方式:

let parameters: Parameters = ["foo": "bar"]

Alamofire.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default)
    .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
        print("Progress: \(progress.fractionCompleted)")
    }
    .validate { request, response, data in
        // Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
        return .success
    }
    .responseJSON { response in
        debugPrint(response)
    }

要了解更多信息,click here