2021年2月5日星期五

System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]

#SalesforceChallenge

I'm trying to escape a string but I had no success so far.

This is the response body I'm getting:

{"text":"this \"is something\" I wrote"}  

Please note that there are 2 backslashes to escape the double quotes char. (This is a sample. Actually I have a big to escape with lots of "text" elements.) When I try to deserialize it I get the following error:

System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]  

I've tried to escape by using:

String my = '{"text":"this \"is something\" I wrote"}';  System.debug('test 1: ' + my.replace('\"', '-'));  System.debug('test 2: ' + my.replace('\\"', '-'));  System.debug('test 3: ' + my.replace('\\\"', '-'));  System.debug('test 4: ' + my.replace('\\\\"', '-'));  

--- Results:

[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}  [24]|DEBUG|test 2: {"text":"this "is something" I wrote"}  [25]|DEBUG|test 3: {"text":"this "is something" I wrote"}  [26]|DEBUG|test 4: {"text":"this "is something" I wrote"}  

--- What I need as result:

{"text":"this -is something- I wrote"}  

Please, does someone has any fix to share?

Thanks a lot.

https://stackoverflow.com/questions/66072249/system-jsonexception-unexpected-character-i-code-105-was-expecting-comma February 06, 2021 at 08:07AM

没有评论:

发表评论