使用新的Swift 3和AlamofireparsingJSON

我使用Alamofire作为HTTP库,因为Swift 3的更新,如何根据下面的例子来parsingJSON?

Alamofire.request("https://httpbin.org/get").responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") } } 

respone.result.value是任何对象,是非常新的和混乱的。

正如你在Alamofiretesting中看到的,你应该把response.result.value 强制转换为[String:Any]

 if let json = response.result.value as? [String: Any] { // ... } 

如果您不想使用SwiftyJson,请使用Alamofire 4.0来执行此操作:

 Alamofire.request("https://httpbin.org/get").responseString { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") } } 

关键是使用responseString而不是responseJSON

来源: https : //github.com/Alamofire/Alamofire#response-string-handler

更新为swift 3:

如果您的回复如下所示,

 [ { "uId": 1156, "firstName": "Kunal", "lastName": "jadhav", "email": "kunal@gmail.com", "mobile": "7612345631", "subuserid": 4, "balance": 0 } ] 

**如果你想parsing上面简单的代码行下面使用的JSON响应:**

  Alamofire.request(yourURLString, method: .get, encoding: JSONEncoding.default) .responseJSON { response in debugPrint(response) if let data = response.result.value{ if (data as? [[String : AnyObject]]) != nil{ if let dictionaryArray = data as? Array<Dictionary<String, AnyObject?>> { if dictionaryArray.count > 0 { for i in 0..<dictionaryArray.count{ let Object = dictionaryArray[i] if let email = Object["email"] as? String{ print("Email: \(email)") } if let uId = Object["uId"] as? Int{ print("User Id: \(uId)") } // like that you can do for remaining... } } } } } else { let error = (response.result.value as? [[String : AnyObject]]) print(error as Any) } }