I have this code in Android:
Map<String, String> idObject1 = new LinkedHashMap<String, String>();
idObject1.put("Id", "Test1");
idObject1.put("FirstName", "Test Name");
idObject1.put("LastName", "Test Surname");
idObject1.put("PhoneNumber", "12345");
Gson gson = new Gson();
json.put("ParticipantIds", gson.toJson(idObject1));
Log.d("Json", json.toString());
Log.d("ParticipantsJson", gson.toJson(idObject1).toString());
The json object also gets other things added to it via the .put method. It’s only messing up at the “ParticipantIds” key.
json.toString()
Outputs
{"ParticipantIds":"{\"Id\":\"Test1\",\"FirstName\":\"Test Name\",\"LastName\":\"Test Surname\",\"PhoneNumber\":\"12345\"}",
and
gson.toJson(idObject1).toString());
Outputs
{"Id":"Test1","FirstName":"Test Name","LastName":"Test Surname","PhoneNumber":"12345"}
I need to remove the \ occurances in the json object when idObject1 is passed into it. It doesn’t necessarily have to use Gson. I only used Gson in an attempt to get it to work. I’ve also tried:
json.put("ParticipantIds", idObject1);
and tried using JSON-Simple to pass it in. I always get the \ escaped character.
I’ve even tried doing .toString() on idObject1 then doing all manner of .replace and .replaceAll methods on it. It doesn’t work!
when u get JsonString replace that escape sequence as given below this works for removing escape sequences