john
john

Reputation: 11669

How to have properly formatted json string with one space after comma using gson?

In my below code, I am making a json string using gson:

private String generateData(Map<String, Map<Integer, Set<Integer>>> nodeTable, int i) {
    JsonObject jsonObject = new JsonObject();

    Set<Integer> pp = nodeTable.get("TEXTER").get(i);
    Set<Integer> sp = nodeTable.get("PETER").get(i);

    // my above pp and sp variables shows correct values with one space between numbers.

    jsonObject.addProperty("description", "Hello. World");
    jsonObject.add("data1", gson.toJsonTree(pp));
    jsonObject.add("data2", gson.toJsonTree(sp));

    System.out.println(jsonObject.toString());

    return jsonObject.toString();
}

When I get my json string, I get it like this. As you can see after comma everything is next to each other without any spaces. I don't want like that.

{"description":"Hello. World.","data1":[0,273,546,819,1092,559],"data2":[816,1644,1368,276]}

I want my json string to be like this: Meaning after comma there should be one space so it should look like below.

{"description":"Hello. World.", "data1":[0, 273, 546, 819, 1092, 559], "data2":[816, 1644, 1368, 276]}

How can I do this?

Update:-

I tried this but it doesn't work:

private String generateData(Map<String, Map<Integer, Set<Integer>>> nodeTable, int i) {

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder = gsonBuilder.setPrettyPrinting();   //Sets pretty formatting
    Gson gson = gsonBuilder.create();       

    JsonObject jsonObject = new JsonObject();

    Set<Integer> pp = nodeTable.get("TEXTER").get(i);
    Set<Integer> sp = nodeTable.get("PETER").get(i);

    // my above pp and sp variables shows correct values with one space between numbers.

    jsonObject.addProperty("description", "Hello. World");
    jsonObject.add("data1", gson.toJsonTree(pp));
    jsonObject.add("data2", gson.toJsonTree(sp));

    System.out.println(gson.toJson(jsonObject));

    return gson.toJson(jsonObject);
}

Upvotes: 1

Views: 4237

Answers (1)

rajuGT
rajuGT

Reputation: 6404

The JSON string should not include unwanted whites paces because in most of the cases it is the medium to transport data, otherwise it will add bandwidth cost(may be small but considered) and also parsing white spaces in client side.

But still if you format it to print pretty or to look readable, you can make use of GsonBuilder to set it as shown below

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder.setPrettyPrinting();   //Sets pretty formatting
Gson gson = gsonBuilder.create();                //Create Gson reference

System.out.println(gson.toJson(object));        //prints in pretty format

Output:

{
  "a": 5,
  "name": "Hello.world.",
  "someOtherString": "some other string"
}

Updated question:

I'm not sure the method parameter's type, so in below sample program I used string, so it did work. I suggest you to change from jsonObject.add("data1", gson.toJsonTree(pp)); to jsonObject.addProperty("data1", gson.toJson(pp));. This may solve, let me know.

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder.setPrettyPrinting();   //Sets pretty formatting
Gson gson = gsonBuilder.create();                //Create Gson reference

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("description", "Hello. World");
jsonObject.addProperty("asfaf", "New. World");
jsonObject.addProperty("asfa", "Old. World");

System.out.println(gson.toJson(jsonObject));

Output

{
  "description": "Hello. World",
  "asfaf": "New. World",
  "asfa": "Old. World"
}

Upvotes: 2

Related Questions