HTTP POST Json格式并设置Alamofire

时间:2018-10-19 09:20:28

标签: ios json swift alamofire

["client_ID": "0", "username": "asdf@gmail.com", "grant_type": "password", "password": "12345", "client_secret": "xxxxxxxxxxxxxxxxxxxxx", "scope": "adfs.user"]

格式转换为此

{"client_ID": "2", "username": "asdf@gmail.com", "grant_type": "password", "password": "12345", "client_secret": "xxxxxxxxxxxxxxxxxxxxx", "scope": "adfs.user"}

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

let params: Parameters = [ "grant_type" : GRANT_TYPE ,"client_ID" : CLIENT_ID ,"client_secret" : CLIENT_SECRET,"username" : emailString, "password" : passwordString,"scope" : ROLE] 
let req = Alamofire.request(url, method: .post, parameters: params, encoding:JSONEncoding.default , headers: nil).validate().responseJSON { (response) in

})

答案 1 :(得分:-1)

如果您使用的是SwiftyJSON,则只需

JSON(postString)

否则,您可以做类似

的操作
do {

//Convert to Data
let jsonData = try JSONSerialization.data(withJSONObject: dictionaryOrArray, options: JSONSerialization.WritingOptions.prettyPrinted)

//Convert back to string. Usually only do this for debugging
if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
   print(JSONString)
}

//In production, you usually want to try and cast as the root data structure. Here we are casting as a dictionary. If the root object is an array cast as [Any].
var json = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any]
} catch {
    print(error.description)
}

有关本机方式的更多信息,请参见Convert array to JSON string in swift