Alamofire.request("https://httpbin.org/get").responseJSON { response in
debugPrint(response)
if let json = response.result.value {
let ip = json["url"] as! String
}
}
Error : 馃憥 Type 'Any' has no subscript members
if response.result.value is NSNull {
return
}
let JSON = response.result.value as? NSDictionary
let id = JSON?["id"] as! String
I think you need the ? before ["url"]
This is how I always handle my JSON responses.
.responseJSON {
response in
switch response.result {
case .failure(let error):
// Do whatever here
return
case .success(let data):
// First make sure you got back a dictionary if that's what you expect
guard let json = data as? [String : AnyObject] else {
NSAlert.okWithMessage("Failed to get expected response from webserver.")
return
}
// Then make sure you get the actual key/value types you expect
guard var points = json["points"] as? Double,
let additions = json["additions"] as? [[String : AnyObject]],
let used = json["used"] as? [[String : AnyObject]] else {
NSAlert.okWithMessage("Failed to get data from webserver")
return
}
Thanks @grosch! 馃嵒
Most helpful comment
This is how I always handle my JSON responses.