How to remove optionals and array symbols from my JSON response?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

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

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs