JSONarray到String没有“

时间:2017-03-24 17:15:09

标签: java json

如何从json数组中删除“,”? 想到一个正则表达式,但我不熟悉如何过滤它或者可能有一个更简单的方法?

我当前的输出是:["ceratia","direwolf1102","fctwentegirl","nightbot","onecat","sohappybot"]

我需要它:

ceratia direwolf1102 fctwentegirl nightbot onecat sohappybot

1 个答案:

答案 0 :(得分:2)

我会避免操纵用[方法创建的字符串,因为很容易删除你不想要的东西。假设您的令牌包含]replace("]","")。这意味着您无法简单地调用],因为它会从字符串值中删除所有",包括一个","JSONArray arr = new JSONArray(); arr.put("foo"); arr.put("bar"); System.out.println(arr);//to show incorrect format StringJoiner sj = new StringJoiner(" "); for (int i = 0; i < arr.length(); i++){ sj.add(arr.getString(i)); } System.out.println(sj); ["foo","bar"] foo bar 相同。

而是简单地迭代你得到的数组,将所有标记放在StringJoiner中,使用空格作为分隔符,然后打印该连接符。

   5|Y x x x Y             Dictionary // a, b, c, d, e could be colours or other atomic values mapped.
   4|x x Y Y x             a -> 1
   3|Y x Y Y Y             b -> 2
   2|Y Y x x x             c -> 3
   1|Y Y Y x Y             d -> 4
     - - - - -             e -> 5
     1 2 3 4 5

x -> represents there's no link (edge) between the nodes.
Y -> represents there's a link (edge) between the nodes.

输出:

{{1}}
相关问题