Alamofire swift 3中的JSON.dictionaryObject为空
我有一个JSON,格式如下:Alamofire swift 3中的JSON.dictionaryObject为空,json,swift,alamofire,swifty-json,Json,Swift,Alamofire,Swifty Json,我有一个JSON,格式如下: postJson ={"firstname":"Vishal","lastname":"raskar","username":"vishal123","password":"123456","confirmpassword":"123456","email":"raskarvishal7@gmail.com","timezone":"1","accountid":"12345","phoneno":"8655012753"} (postJson的数据类型为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访问服务器,所以我需要在参数中以字典格式发布JSON数据:\uuuuuuuuuu
,例如:
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
获取null(即使数据存在于postJson
中)
我尝试了所有组合,如postJson.dictionaryValue
,postJson.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)
}
}
然后通过Alamofire发布,现在得到回复。我做错了什么?我想使用我的第一个选项。假设您使用的是Swift 3/Alamofire 4.0 以下是根据Alamofire文档定义“参数”参数的方式:
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)
}
要了解更多信息,您可以直接使用[“firstname”:“Vishal”,“lastname”:“raskar”,“username”:“vishal123”,“password”:“123456”,“confirmpassword”:“123456”,“email”:raskarvishal7@gmail.com“,”时区“:”1“,”帐户ID“:”12345“,”电话号码“:”8655012753”]
作为请求的参数,无需将其转换为swiftyjson对象。