Reputation: 286
My JSON object is like: {ids:[2079]}
, but when I'm trying to get String value using
Gson gson = new GsonBuilder().setExclusionStrategies(new GsonExclusionStrategy()).create();
gson.toJson(body);
gson.toJson() is printing "{\"ids\":[2079]}"
, where as the expected behaviour is {\"ids\":[2079]}
Upvotes: 2
Views: 4914
Reputation:
I think you are double calling toJson() method which is already a String.
I mean if you call toJson() on "{ids:[2079]}"
, then it will produce "{\"ids\":[2079]}"
. So check your object.
Upvotes: 9
Reputation: 381
I gone through same problem but finally i got solution. i added commons-lang3-3.4 JAVA Library.download library
And used sample code.
String varibleString = StringEscapeUtils.unescapeJava(your JSON response string);
varibleString = varibleString .substring(1, varibleString .length() - 1);
try {
json = new JSONObject(varibleString );
}catch(Exception e)
{
e.printStackTrace();
}
StringEscapeUtils.unescapeJava(your JSON response string); this method will help you to remove unwanted character from JSON Strting.
Upvotes: 0