0
class Request {     
     private String requestid;
     private String contenttype;
     private String service;
     private String requestjson;
}

making this object to json using Object mapper but the mapper is returning me

"requestjson\":\"{\\\"username\\\":\\\"farhan\\\",\\\"password\\\":\\\"farhaan\\\"}\"}"}

How can I remove this extra

\\\

I tried

 jsonOutput.replaceAll("\"",Character.toString ((char) 34));
Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
Vinod Bokare
  • 86
  • 3
  • 8
  • 4
    What is the code that you are using to make JSON out of that object! – aksappy Dec 01 '15 at 11:27
  • [`replaceAll`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29) is for regular expressions. You mean [`replace`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29). – khelwood Dec 01 '15 at 11:29
  • i was trying with replaceAll("\\",""); it will remove my backslash but its not removing – Vinod Bokare Dec 01 '15 at 11:53
  • 2
    http://stackoverflow.com/questions/13939925/remove-all-occurrences-of-from-string – Maas Dec 01 '15 at 11:54
  • I'm not sure you should remove it. It looks like it's been encoded multiple times, so you should decode it multiple times. – Boann Dec 01 '15 at 12:04

1 Answers1

5

You can try this.

String jsonString = jsonStr.replaceAll("\\\\", "");

The reason you have to double up the (already doubled) backslashes is that replaceAll takes a regular expression and a single backslash is used in regex.

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
Harshil
  • 463
  • 1
  • 8
  • 28
  • If you're not using the features of regular expressions, you shouldn't be using `replaceAll`. That is what [`replace`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29) is for – khelwood Dec 01 '15 at 14:52