Rohit Rai
Rohit Rai

Reputation: 286

Gson.toJson adding extra quotes to string value of JSON

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

Answers (2)

user5349506
user5349506

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

Amey Bawiskar
Amey Bawiskar

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

Related Questions