I have a string with escape characters in such a way that the when the string is printed it results in another string with valid escape characters. How do i retrieve that string which looks like the one when the first string is printed?
Here’s the code – the initial string looks as such:
String string = "{\"agent\":\"{\\\"name\\\":\\\"James Bond\\\"}\"}";
System.out.println("str: "+string);
Executing the code would produce
str: {"agent":"{\"name\":\"James Bond\"}"}
I was to get the string as it appears in the output.
You can use the
String unescapeJava(String)method ofStringEscapeUtilsfrom Apache Commons Lang.example:
You can see more on How to unescape a Java string literal in Java?