How to remove optionals and array symbols from my JSON response?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
How to remove optionals and array symbols from my JSON response?
I am using below HTTP Post call and I am getting response with unnecessary symbols, how to remove those symbols also how to store those values with variables.
let task = URLSession.shared.dataTask(with: request) data, response, error in
guard let data = data, error == nil else // check for fundamental networking error
print("error=(error)")
return
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 // check for http errors
print("statusCode should be 200, but is (httpStatus.statusCode)")
print("response = (response)")
let responseString = String(data: data, encoding: .utf8)
print("responseString = (responseString)")
task.resume()
My output response:
Optionals(""name":"juju","result":"win"")
1 Answer
1
Using String(data:encoding:) will create an optional string. All you need is unwrap the optional using a guard let
statement.
guard let
guard let responseString = String(data: data, encoding: .utf8) else return
print("responseString = (responseString)")
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment