"Text1/Text2" String is replaced by "Text1\/Text2" in android

android, json

Solution

As @GrlHu said that by default the android will convert your string into utf-8 encoding format so your `/` will be replaced with `\/`. Please read the following two post 1. JSON: why are forward slashes escaped? 2. Why is the slash an escapable character in JSON? So instead of this you can use `getString(Name)` method. Hope you will get the perfect value.

try {
    String str;
    JSONObject json = new JSONObject();

    json.put("Name", "Text1/Text2");
    str = json.getString("Name");
    Log.e("test", str);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Problem

When i try to run this code in android i am getting the resultant String as `"Name":"Text1\/Text2"` But the result should be `{"Name":"Text1/Text2"}`. ``` try { String str; JSONObject json = new JSONObject(); json.put("Name", "Text1/Text2"); str = json.toString(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } ``` Thanks.

Original source

Related problems