1

I am writing a rest api.All things are set and woking fine. My controller :

@RequestMapping(value = "/getDetails", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getDetails(@RequestBody String jsonInput) {
    LOGGER.info("<------------------------ from Date   :" + jsonInput );
    System.out.println("resp ="+apiService.getDetails(jsonInput));
    return apiService.getDetails(jsonInput);

}

When I sysout the response on console it will print normal string like -

[{"name": "abc","vendor": "abc df"}]

But I get the respnse with extra slash. All double quote is replaced by \".

"[{\"name\":\"abc\",\"vendor\":\"abc df\"}]"

Please help !. Thanks in advance.

Samir
  • 4,940
  • 3
  • 13
  • 25
  • 1
    This is expected behavior. The `\ ` escapes special characters so that they can be printed. Inspecting a `Map()` object might reveal a similar outcome. – j.seashell Jan 03 '17 at 18:38
  • i have found a more but subtle question , http://stackoverflow.com/questions/3020094/how-should-i-escape-strings-in-json – Pavneet_Singh Jan 03 '17 at 18:44
  • Already done this,when converting string to object to json Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonRes = gson.toJson(resp); – Samir Jan 03 '17 at 18:46
  • @j.seashell : I know this is the expected, but I am searching the way to remove slash. – Samir Jan 03 '17 at 18:53
  • When I hit the api from postman tool ,the response looks weird. – Samir Jan 03 '17 at 18:54
  • Why are you getBillingDetails in print and getDetails in return? – John Down Jan 03 '17 at 19:10
  • 1
    What is "looks weird"? You should NOT do what you're trying to do. JSON specifications require `"` to be escaped. Not to mention, how is Javascript supposed to interpret this response if the quotes aren't escaped? – Christopher Schneider Jan 03 '17 at 19:10
  • @Christopher Schneider : I got it. Thank you. – Samir Jan 03 '17 at 19:18

3 Answers3

0

This is expected so the object can be reconstructed later on. The "\" escapes the quote. You can replace the "\" with a empty string:

@ResponseBody
public String getDetails(@RequestBody String jsonInput) {
    LOGGER.info("<------------------------ from Date   :" + jsonInput );
    System.out.println("resp ="+apiService.getBillingDetails(jsonInput));
    return apiService.getDetails(jsonInput).replace("\\", "");

}
John Down
  • 408
  • 1
  • 5
  • 17
  • 1
    No. It's not working. :( This is what I am telling when I print the sysout it printing the normal JSON. But I am getting extra slash in postman tool or any other rest api hitting tool. – Samir Jan 03 '17 at 19:05
0

use

string ResponseString = Regex.Unescape(oldString);

This should remove all the unnecessary escape characters.

Abhiram Chennuru
  • 441
  • 3
  • 11
0

I recently came across the same issue. I was trying to fix it for a long time and finally found the fix and what I was doing wrong. I was framing multiple json strings using Jackson library and storing it into a list.

When I tried to return the jsonList as such to my controller, I was getting the ouput printed on my postman API with double quotes and backslashes, although I did not have this issue while printing to console.

When I tried to return my list as String to the controller fixed my issue.

jsonString = frameJsonKeyValuePair(header, dataList);
if (jsonString != null)
        jsonStringList.add(jsonString);
}
return jsonStringList.toString();
dom
  • 630
  • 4
  • 16