4

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.

Krease
  • 15,805
  • 8
  • 54
  • 86
ravi
  • 2,722
  • 7
  • 25
  • 42

1 Answers1

5

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();
}
Community
  • 1
  • 1
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70