使用新的Swift 3和Alamofire解析JSON

Law*_*nez 11 ios swift alamofire swift3

我使用Alamofire作为HTTP库,因为更新到Swift 3,你如何根据下面的例子解析JSON?

Alamofire.request("https://httpbin.org/get").responseJSON { response in
    debugPrint(response)

    if let json = response.result.value {
        print("JSON: \(json)")
    }
}
Run Code Online (Sandbox Code Playgroud)

respone.result.value 是任何对象,并且是非常新的和令人困惑的.

mix*_*xel 19

正如您在Alamofire测试中所看到的,您应该投射response.result.value[String:Any]:

if let json = response.result.value as? [String: Any] {
  // ...
}
Run Code Online (Sandbox Code Playgroud)