Search code examples
javascriptjavajsonparse-platformstringify

Converting javascript stringified string to java string


I have an app that uses Parse as a backend. Parse calls a web scraper which returns a string to Parse and I have to use stringify on the string I received because it's not always allowed to be sent over the network otherwise. Once I stringify that string how can I parse it in Java so that I have the original returned string. For instance, how do I go from this stringified string in javascript

"\"This is a string\"\r\n\t\t"

To this string in java

"This is a string"

To reiterate, I start with a String (such as "This is a string") but then have to stringify that to send it over the network (result "\"This is a string\"\r\n\t\t"). Once I return the stringified string to my android app, how can I get back the original string ("This is a string")?


Solution

  • Try StringEscapeUtils from apache commons

    String input = "\"This is a string\"\r\n\t\t";
    
    String output = StringEscapeUtils.unescapeJava(input);
    
    System.out.println(output);
    
    "This is a string"