7

i need some help for Remove Back slash from Json String.

Here is the Response which i Get From Server side.

"response":"{\"time_zone\":\"\",\"session_token\":\"abcdefgHijklomopqrstuvwxyz\",\"user_login\":\"abc\",\"user_profile_img\":\"http://jhjhjhj.org/system/photos/62/medium/images.jpg?1462446436\",\"success\":\"0\",\"org_admin\":\"\",\"user_id\":\"62\",\"user_org_id\":\"101\",\"phone_mobile\":\"510-427-9639\",\"user_email\":\"[email protected]\"}"}

what i have don for Remove Backslash from this String

result.replaceAll("\\","");

than After it will give me This String which is not in Json Formate.

{"response":"{"time_zone":"","session_token":"nskfndkjfsfsdffjsdfd","user_login":"newoff2","user_profile_img":"http://absdds.org/system/photos/62/medium/images.jpg?1462446436","success":"0","org_admin":"","user_id":"62","user_org_id":"101","phone_mobile":"510-427-9639","user_email":"[email protected]"}"}

it Give me Exaption

    org.json.JSONException: Unterminated object at character 17 of 

{"response":"{"time_zone":"","session_token":"kjshhdscncnxzcxclx","user_login":"newoff2","user_profile_img":"http://abcd.org/system/photos/62/medium/images.jpg?1462446436","success":"0","org_admin":"","user_id":"62","user_org_id":"101","phone_mobile":"510-427-9898","user_email":"[email protected]"}"}

How Can i remove this Back slash with Proper Json Formate ?

Thanks in advance

5
  • 2
    Fix the server from escaping the quotes... Don't fix the Java code Commented Jul 31, 2016 at 19:14
  • Exactly, do not edit the response code, find a converter or a correct json reader. Or for the best practice, fix the code for webservice side. Commented Jul 31, 2016 at 19:17
  • Maybe, jodatime may fix your problem. Commented Jul 31, 2016 at 19:18
  • @RushDroid Did you manage to solve this issue in Android Side? or did u fix it in Server side? Commented Oct 17, 2017 at 17:54
  • @hemanthkumar i resolve this issue from android side. Commented Oct 24, 2017 at 5:51

4 Answers 4

7

The command

result.replaceAll("\\","");

is correct and if you try to display the server response with an online json formatter (https://jsonformatter.curiousconcept.com/) you can clearly see that the string is not correctly formatted. The double quotes after the "response" and one at the end are not required.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that will work. What if the JSON data actually had a backslash in a string, for example?
I agree with you @cricket_007, but I was focused on the specific example RushDroid provided
It will not work most cases where the json itself contains a backslash as its value such as a web url. The url value then will be destroyed completely. Wrong answer is accepted. I posted the proper answer in below post. Alas the questioner does not know that what dangerous code he pushed into his project.
2

UPDATED: August 2019 Use google's Gson for parsing to Json by omitting backslash automatically:

JAVA CODE:

    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse("your jsonString with backslash").getAsJsonObject();

KOTLIN CODE:

    val jsonParser = JsonParser()
    val jsonObject = jsonParser.parse("your jsonString with backslash").asJsonObject

Comments

0

An alternative to remove backslashes from JSON string.

s = Your String with Back Slash

For converting String to JSONObject:

JSONObject jsonObject = new JSONObject(s);

For converting String to JSONArray:

JSONArray jsonArray = new JSONArray(s);

Comments

0

You can try this and it will work fine

String jsonStr4 = jsonStr3.replaceAll("\"", "");

1 Comment

Could you please add an explanation to your answer to help future readers?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.