Answer the question
In order to leave comments, you need to log in
How to process JSON data received with Alamofire in Swift3?
Two libraries are connected: Alamofire 4.4 and SwiftyJSON.
Using the example from the documentation, I received data from the server in utf8:
Alamofire.request(getCitiesURL).responseData { response in
debugPrint("All Response Info: \(response)")
if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
}
}
if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text["id"])")
}
Answer the question
In order to leave comments, you need to log in
That's right, because `utf8Text` is a constant of type String, you can't access it like this: utf8Text["id"].
You need to cast response.value to dictionary [String: Any] or, even simpler, use responseJSON instead of responseData:
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question