Remove back slash from Json String in android

醉酒当歌 提交于 2019-12-07 12:12:27

问题


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\":\"abc@pdmoffice.com\"}"}

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":"hjhjh@pdmoffice.com"}"}

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":"sdgas@pdmoffice.com"}"}

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

Thanks in advance


回答1:


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.




回答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


来源:https://stackoverflow.com/questions/38687040/remove-back-slash-from-json-string-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!