【问题标题】:How to give one space between number in json string java?java - 如何在json字符串java中的数字之间留一个空格?
【发布时间】:2016-02-12 14:15:22
【问题描述】:

在下面的代码中,我正在使用 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]}

我希望 data1 和 data2 变量中的数字之间有一个空格,如下所示:

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

是否有可能使用 gson 或其他方式来做到这一点?

【问题讨论】:

  • 你绑定到库中toString 方法的实现。为什么你喜欢这个不同?处理没关系
  • 我正在将 data1 和 data2 传递给我的 python 程序,他们希望它之间有空格。我能想到的最简单的方法是更改​​此代码而不是更改 python 代码,然后我需要 bcoz更改我们所有的服务器,这是不可能的,所以这就是为什么我想尽可能更改此代码,因为我们可以轻松部署此代码。
  • 错误缠身的方法只是在整数的逗号后放置空格,如果它是字符串,为什么那里有数字?仔细考虑。
  • 它不会是字符串,它永远是整数。

标签: java json regex string gson


【解决方案1】:

这似乎是一个可能存在错误的装饰性要求。最简单的方法可能是使用正则表达式:

jsonString = jsonString
    .replaceAll("(\\d),(\\d)", "$1, $2")
    .replaceAll("(\\d),(\\d)", "$1, $2")

上面的行简单地捕获任何数字 + 逗号 + 数字序列,并在两个捕获和恢复的数字之间添加一个空格。由于java只捕获一次匹配,因此不允许交叉,我们在这里做了两次。

不那么严格但不需要双重替换的替代方案:

jsonString = jsonString.replaceAll(",(\\d)", ", $1")

这里的潜在错误是这适用于整个 JSON 字符串,而不仅仅是编码集。

正确的方法™ 是为 GSON 使用自定义设置格式化程序。

顺便说一句,地图的地图呼唤班级。

【讨论】:

  • 你能举例说明正则表达式在我的情况下如何工作吗?
  • 完成。 jsonString 是你的 jsonObject.toString() 的输出
  • 它只是在第一个数字后添加空格。 {"description":"Hello. World","data1":[1, 2,3],"data2":[4, 5,6]}
  • 在正则表达式编译时出现非法转义字符问题,更新代码为 jsonString.replaceAll("(\\d),(\\d)", "$1, $2")
  • 我只有在第一个数字之后才获得空间。之后它不起作用。这是它打印出来的{"description":"Hello. World","data1":[1, 2,3],"data2":[4, 5,6]}
【解决方案2】:

使用以下方法获取空间的最简单方法

For 1:
jsonObject.add("data1", gson.toJsonTree(addSpaces(pp)));
jsonObject.add("data2", gson.toJsonTree(addSpaces(sp)))

For 2: 
String string = addSpaceByRegEx(jsonObject.toString());

在你的类中添加以下方法:

//1. if you need String value based spaces in json string
public Set<String> addSpaces(Set<Integer> integers) {
    Set<String> strings = new HashSet<>();
    for (Integer i : integers) {
        strings.add(i.toString() + " ");
    }
    return strings;
}

//2. if you need space as in integer value in json string as your example
//This method add space by using regex 
//Tested for String s = "{'description':'Hello. World.','data1':[0,273,546,819,1092,559],'data2':[816,1644,1368,276]}";
//In you example just replace following line
//System.out.println(jsonObject.toString()); by System.out.println(addSpaceByRegEx(jsonObject.toString()));
//See result, i think it work
public String addSpaceByRegEx(String jsonString) {
    Pattern pattern = Pattern.compile(",[0-9]");
    Matcher matcher = pattern.matcher(jsonString);
    StringBuilder sb = new StringBuilder();
    int prevIndex = 0;
    while (matcher.find()) {
        int startIndex = matcher.start();
        int endIndex = matcher.end();
        sb.append(jsonString.substring(prevIndex, startIndex + 1)).append(" ").append(jsonString.substring(startIndex + 1, endIndex));
        prevIndex = endIndex;
    }
    sb.append(jsonString.substring(prevIndex, jsonString.length()));
    return sb.toString();
}

【讨论】:

  • 它像这样打印{"description":"Hello. World","data1":["2 ","1 ","3 "],"data2":["4 ","6 ","5 "]} 正如您在我的问题中看到的那样 - 我在 data1 和 data2 中没有双引号字符串数字。
  • 好的,我需要几分钟。
  • 感谢它几乎可以工作。但是当我运行它时,它会像这样打印出来{"description":"Hello. World","data1":[1 ,2 ,3],"data2":[4 ,5 ,6]}。我在data1中寻找类似1, 2, 3的东西,在data2中寻找4, 5, 6。在您的输出中,它会在数字前打印逗号。知道有什么问题吗?
  • 好的,我需要几分钟,我会根据您的要求进行更正。
  • 我根据您的要求更新代码,如果可行,请接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2012-02-08
  • 2020-05-18
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多