From an API, I get a json with some escape character backslashes. Here is an example:
https://store.steampowered.com/api/appdetails?appids=294100
When I try to decode with a JSONDecoder()
, I get NSCocoaErrorDomain Code=3840 "Unexpected character . . ."
pointing to the character after the first backslash.
In a playground, I can get around this by padding with a “#” before and after the multiline literal string, which I believe makes it a raw string, and it then parses successfully! Wowee!
My problem is: I am not sure how to tell Mr. JSONDecoder() to treat the data as a raw string when I am not in direct control of the incoming api response.
Alternatively: I am not sure how to successfully pad/wrap an incoming api response with the “#” character, so the decoder will treat it as a raw string and my life can move forward.
I have been unsuccessfully trying to pad (e.g.: "#".data(using: .utf8)!
around the API response with concatenation) before I .decode
it.
I am not sure how to cast data to a string with something like .base64EncodedString()
, which is at this moment moot, since I am unable to verify because the decoder doesn’t like the #, and now gives the Unexpected character for that character.
This puzzles me since, in the playground, I simply surround the mock multiline literal json (which is the above API response as a multiline literal string, appended with .data(using: .utf8)!
) with “#”, and it parses into my objects, and life is once again full of joy and realized potential.
Obligatory “I have spent more than a few stubborn hours on this, reviewed all the suggestions as I write this question, and I am sure it is something simple, so I am sorry if it is a duplicate”
Cheers!
OrYouCouldJustDoTheThing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.