0

I need to remove "\" character from json string after executing GET command in java

I tried to remove by replace method but unable to remove

response = ["{ \"isEnriched\":\"true\",\"event\":{\"commonEventHeader\":{\"startEpochMicrosec\":\"1555099630557000\"}}}"] responseBody = response.replaceAll("\", " ");

Unable to replace "\" with blank space

Rohit
  • 23
  • 5
  • When using `'\'` in strings, you need to escape it: `'\\'`. And since [`replaceAll`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)) uses regex, you need to escape it again: `'\\\\'`. Though, you might be better off using a real json parser. [How to parse JSON in Java](//stackoverflow.com/q/2591098) – 001 Sep 05 '19 at 13:27
  • Can you post an actual snippet of Java code? Your example doesn't seem to compile. – arcadeblast77 Sep 05 '19 at 13:30
  • 1
    Also note there are actually no backslashes in the text you have provided. Those `\"` are escaped double quotes. – 001 Sep 05 '19 at 13:34
  • 2
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – pringi Sep 05 '19 at 14:00

2 Answers2

1

this seems to work:

 "{\"isEnriched\":\"true\",\"event\":{\"commonEventHeader\":{\"startEpochMicrosec\":\"1555099630557000\"}}}".replace("\\", "");

you need to use "\\" because \ is an escape character.

Syrup72
  • 116
  • 1
  • 12
0

You need to use response.replaceAll("\\\\", "").

In Java \ is escape character. Thus to escape it you need one \ and to make it a '\' you need 2 \ to escape each \

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23