Reputation: 51
I have large json string something like this:
[{\"name\":\"Nick\",\"role\":\"admin\",\"age\":\"32\",\"rating\":47}]
I want to remove every occurrence of \" with " in string.
for this i used String's `relaceAll("\\"","\"") when i am print the string after replace its printing fine but when i am sending string to object in json. its appending slash , please guide me how to get rid of this slash
My expecting result:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
Upvotes: 2
Views: 2023
Reputation: 9648
For this i used String's
relaceAll("\\"","\"")
...
The String#replaceAll() method interprets the argument as a RegEx (Regular Expression). The Backslash Character (\) is an escape character in both String & Regex.
Hence, you need to double-escape it for the RegEx to work.
Example:
myString = myString.replaceAll("\\\\", "\\\\\\\\");
You can also use String#replace() method to perfrom the same task like this:
myString = myString.replace("\\", "\\\\");
Upvotes: 1