相关文章推荐
憨厚的单杠  ·  C语言转义字符·  1 年前    · 
瘦瘦的钢笔  ·  hive union all ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

#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 0: ' + my);
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:

[22]|DEBUG|test 0: {"text":"this "is something" I wrote"}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[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.

This is the problem with your test runs in Anonymous Apex:

String my = '{"text":"this \"is something\" I wrote"}';

Because \ is an escape character, you need two backslashes in an Apex string literal to produce a backslash in the actual output:

String my = '{"text":"this \\"is something\\" I wrote"}';

Since Apex quotes strings with ', you don't have to escape the quotes for Apex; you're escaping them for the JSON parser.

The same principle applies to the strings you're trying to use to do replacements: you must escape the \ for Apex.

All that said, it's unclear why you are trying to manually alter this string. The payload

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

is valid JSON. In general, you should not perform string replacement on inbound JSON structures in Apex unless you're attempting to compensate for a payload that contains an Apex reserved word as a key so that you can use typed deserialization.

Hi David. Thanks for replying. I tried to show the string variable without replacing anything. This was the output: {"text":"this "is something" I wrote"} When I try to deserialize I get an error: Map<String, String> test = (Map<String, String>) JSON.deserialize(my, Map<String, String>.class); Error: System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:17] – Israel Agostinho Feb 6, 2021 at 5:18

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.