Suren Konathala
Suren Konathala

Reputation: 3597

How to avoid backslashes in GSON JsonObject?

I have a Java POJO

public class TagBean {
  private String type;
  private String id;

  public TagBean(String type, String id) {
    this.type = type;
    this.id = id;       
  }
    // getters
    // setters   
}

I'm building pojo's and adding them to a List, as

....
List<TagBean> channelsList = new ArrayList<>();
List<TagBean> showsList = new ArrayList<>();
for each <business logic> {
   if value=channels {
      channelsList.add(new TagBean(...));
   }
   if value=shows {
      showsList.add(new TagBean(...));
   }
}

Gson gson = new GsonBuilder().create();
JsonObject tjsonObject = new JsonObject();
tjsonObject.addProperty("channels", gson.toJson(channelsList));
tjsonObject.addProperty("shows", gson.toJson(showsList));

JsonObject mainjsonObject = mainjsonObject.add("tags", tjsonObject);

return mainjsonObject;

My output is:

{
"tags": {
    "channels": "[{\"type\":\"channel\",\"id\":\"channel\",\"name\":\"Channel\",\"parent\":\"SXM\"}]",
    "shows": "[{\"type\":\"shows\",\"id\":\"shows\",\"name\":\"Shows\",\"parent\":\"SXM\"},{\"type\":\"shows\",\"id\":\"howard\",\"name\":\"Howard Stern\",\"parent\":\"shows\"},{\"type\":\"shows\",\"id\":\"howardstern\",\"name\":\"Howard Stern\",\"parent\":\"howard\"}]",
    "sports": "[]"
}
}

How can i remove the backslashes? So the output is like:

{
  "tags": {
     "channels": " [{"type":"channel","id":"channel","name":"Channel","parent":"SXM"}]",
    "shows": "[{"type":"shows","id":"shows","name":"Shows","parent":"SXM"},{"type":"shows","id":"howard","name":"Howard Stern","parent":"shows"}....

There were few other posts, but none explained this.

Upvotes: 3

Views: 10074

Answers (5)

madhu527
madhu527

Reputation: 4782

All strings in java have to escape quotes in them. So jsonInString should have slashes in it. When you output jsonInString though it shouldn't have the quotes. Are you looking at it in a debugger or something?

Just parse json directly and check - will get the output

Upvotes: 0

umang pandey
umang pandey

Reputation: 1

above solution is not working anymore since GSON 2.8.* use gson.toJsonTree(jsonText).getAsString(); instead

Upvotes: -1

dazhi
dazhi

Reputation: 21

    String mainJsonStr = mainjsonObject.toString();
    mainJsonStr = mainJsonStr.replace("\\\\", ""); //replace the \
    System.out.println(mainJsonStr);

Upvotes: 2

Mukul Bhardwaj
Mukul Bhardwaj

Reputation: 592

The problem is that gson.toJson returns a String, and

tjsonObject.addProperty("channels", gson.toJson(channelsList));

this will add channels as a string and not as a JSON object. One possible solution is to convert the string returned from gson.toJson to JSON object first then add it to the parent JSON object like

Gson gson = new GsonBuilder().create();
JsonObject tjsonObject = new JsonObject();
tjsonObject.put("channels", new JsonObject(gson.toJson(channelsList)));
tjsonObject.put("shows", new JsonObject(gson.toJson(showsList)));

this will treat channels and shows as JSON object

Upvotes: 1

Stephen C
Stephen C

Reputation: 718788

The problem is caused by this:

tjsonObject.addProperty("channels", gson.toJson(channelsList));

What that is doing is converting channelsList to a string containing a representation of the list in JSON, then setting the property to that string. Since the string contains JSON meta-characters, they must be escaped when the strings are serialized ... a second time.

I think that you need to do this instead:

tjsonObject.add("channels", gson.toJsonTree(channelsList));

That should produce this:

{
  "tags": {
     "channels":     
        [{"type":"channel","id":"channel","name":"Channel","parent":"SXM"}],
     "shows": 
        [{"type":"shows","id":"shows","name":"Shows","parent":"SXM"},
         {"type":"shows","id":"howard","name":"Howard Stern","parent":"shows"}
   ....

That is slightly different to what your question asked for, but it has the advantage of being syntactically valid JSON!

Upvotes: 6

Related Questions