2017-04-17 65 views
0

我有以下格式的JSON:JSON.dictionaryObject是空的Alamofire迅速3

postJson ={"firstname":"Vishal","lastname":"raskar","username":"vishal123","password":"123456","confirmpassword":"123456","email":"[email protected]","timezone":"1","accountid":"12345","phoneno":"8655012753"} 

(postJson的數據類型是JSON即swiftyJSON)

現在我想通過Alamofire打在服務器,所以我需要張貼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.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後,現在越來越響應。我究竟做錯了什麼?我想用我的第一個選項。

+0

你可以直接使用'[「firstname」:「Vishal」,「lastname」:「raskar」,「username」:「vishal123」,「password」:「123456」,「confirmpassword」:「123456」,「email」:「raskarvishal7 @ gmail.com「,」timezone「:」1「,」accountid「:」12345「,」phoneno「:」8655012753「]作爲請求參數no需要將其轉換爲swiftyjson對象。 –

回答

0

假設你使用的斯威夫特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) 
    } 

要了解更多信息,click here