How to prettyPrint json response from API call in debugger?
Try the JSONSerialization
, something like this:
let url = URL(string: "http://date.jsontest.com")
var request : URLRequest = URLRequest(url: url!)
request.httpMethod = "GET"
let dataTask = URLSession.shared.dataTask(with: request) {
data,response,error in
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error {
print(error.localizedDescription)
}
}
dataTask.resume()
Where jsonResult
will print out this:
{
date = "02-19-2018";
"milliseconds_since_epoch" = 1519078643223;
time = "10:17:23 PM";
}
e print(String(data: JSONSerialization.data(withJSONObject: JSONSerialization.jsonObject(with: data, options: []), options: .prettyPrinted), encoding: .utf8)!)