如何使用gson在逗号后用一个空格正确格式化json字符串?

时间:2015-11-11 10:35:24

标签: java json gson

在我的下面代码中,我使用gson创建一个json字符串:

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();
}

当我得到我的json字符串时,我就这样得到它。正如你在逗号后看到的,一切都是彼此相邻而没有任何空格。我不想那样。

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

我希望我的json字符串是这样的:逗号之后应该有一个空格,所以它应该如下所示。

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

我该怎么做?

更新: -

我尝试了这个,但它不起作用:

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);
}

1 个答案:

答案 0 :(得分:2)

JSON字符串不应包含不需要的白色节奏,因为在大多数情况下它是传输数据的媒介,否则会增加带宽成本(可能很小但考虑到)并且还会解析白色客户端的空间。

但是如果您将其格式化为漂亮或看起来可读,您仍然可以使用GsonBuilder进行设置,如下所示

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

输出:

jsonObject.add("data1", gson.toJsonTree(pp));

更新了问题:

我不确定方法参数的类型,所以在下面的示例程序中我使用了字符串,所以它确实有效。我建议您从jsonObject.addProperty("data1", gson.toJson(pp));更改为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)); 。这可以解决,让我知道。

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

输出

require 'rubygems'
require 'riak'
STYLES = %w{single double queen king suite}
client = Riak::Client.new(:nodes => [
 {:host => 'localhost', :http_port => 8098}
])

bucket = client.bucket('rooms')
# Create 100 floors to the building
for floor in 1..100
  current_rooms_block = floor * 100
  puts "Making rooms #{current_rooms_block} - #{current_rooms_block + 100}"
  # Put 100 rooms on each floor (huge hotel!)
  for room in 1...100
    # Create a unique room number as the key
    ro = Riak::RObject.new(bucket, "#{current_rooms_block + room}")
    # Randomly grab a room style, and make up a capacity
    style = STYLES[rand(STYLES.length)]
    capacity = rand(8) + 1
    # Store the room information as a JSON value
    ro.content_type = "application/json"
    ro.data = {'style' => style, 'capacity' => capacity}
    ro.store
 end
end