字符串数组到 json 数组

Array of strings to json array

实际上我正在使用 "com.github.spullara.mustache.java" 但这应该不重要。我有字符串数组。如果我尝试使用以下方式渲染它:

{
    "codes": {{variable}}
}

然后它被渲染

{
    "codes": [a, b]
}

接近 OK。但是我们缺少撇号。

有没有办法添加这些?我试过这个:

[{{#codes}}"{{.}}"{{/codes}}]

更接近,但中间漏掉了逗号(该死!)

{
    "codes": ["a""b"]
}

如果我添加逗号:

[{{#codes}}"{{.}}",{{/codes}}]

最后会有(毫不奇怪)额外内容。

有没有办法做到这一点?更改输入数据结构对我来说不是一个选项。

到目前为止,我只能想出一个修剪最后的讨厌的技巧 html。

// after rendering.
var eleHTML = $("#target60").html();
eleHTML = eleHTML.substring(0, eleHTML.length - 6);
$("#target60").html(eleHTML);

试一试最后要剪掉多少。如果你的数据总是相同的长度,你可能会逃避这个。


编辑:

看起来这已经被覆盖了很多。看起来没有内置的东西:

In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma?