当手动生成JSON对象或数组时,通常更容易在对象或数组的最后一项上留下逗号。例如,从字符串数组输出的代码可能像这样(在c++中像伪代码):
s.append("[");
for (i = 0; i < 5; ++i) {
s.appendF("\"%d\",", i);
}
s.append("]");
给你一个字符串
[0,1,2,3,4,5,]
这是允许的吗?
当手动生成JSON对象或数组时,通常更容易在对象或数组的最后一项上留下逗号。例如,从字符串数组输出的代码可能像这样(在c++中像伪代码):
s.append("[");
for (i = 0; i < 5; ++i) {
s.appendF("\"%d\",", i);
}
s.append("]");
给你一个字符串
[0,1,2,3,4,5,]
这是允许的吗?
当前回答
由于for循环用于遍历数组或类似的可迭代数据结构,我们可以使用数组的长度,如下所示,
awk -v header="FirstName,LastName,DOB" '
BEGIN {
FS = ",";
print("[");
columns = split(header, column_names, ",");
}
{ print(" {");
for (i = 1; i < columns; i++) {
printf(" \"%s\":\"%s\",\n", column_names[i], $(i));
}
printf(" \"%s\":\"%s\"\n", column_names[i], $(i));
print(" }");
}
END { print("]"); } ' datafile.txt
datafile.txt包含,
Angela,Baker,2010-05-23
Betty,Crockett,1990-12-07
David,Done,2003-10-31
其他回答
使用relax JSON,您可以使用后面的逗号,也可以不使用逗号。它们是可选的。
在解析类似json的文档时,完全没有必要使用逗号。
看一看relax JSON规范,你会发现原始JSON规范是多么“嘈杂”。太多的逗号和引号……
http://www.relaxedjson.org
您还可以使用这个在线RJSON解析器尝试您的示例,并查看它是否被正确解析。
http://www.relaxedjson.org/docs/converter.html?source=%5B0%2C1%2C2%2C3%2C4%2C5%2C%5D
Using a trailing comma is not allowed for json. A solution I like, which you could do if you're not writing for an external recipient but for your own project, is to just strip (or replace by whitespace) the trailing comma on the receiving end before feeding it to the json parser. I do this for the trailing comma in the outermost json object. The convenient thing is then if you add an object at the end, you don't have to add a comma to the now second last object. This also makes for cleaner diffs if your config file is in a version control system, since it will only show the lines of the stuff you actually added.
char* str = readFile("myConfig.json");
char* chr = strrchr(str, '}') - 1;
int i = 0;
while( chr[i] == ' ' || chr[i] == '\n' ){
i--;
}
if( chr[i] == ',' ) chr[i] = ' ';
JsonParser parser;
parser.parse(str);
不。https://json.org中的“铁路图”是规范的精确翻译,并明确表示a,总是在值之前,而不是直接在值之前]:
或}:
由于for循环用于遍历数组或类似的可迭代数据结构,我们可以使用数组的长度,如下所示,
awk -v header="FirstName,LastName,DOB" '
BEGIN {
FS = ",";
print("[");
columns = split(header, column_names, ",");
}
{ print(" {");
for (i = 1; i < columns; i++) {
printf(" \"%s\":\"%s\",\n", column_names[i], $(i));
}
printf(" \"%s\":\"%s\"\n", column_names[i], $(i));
print(" }");
}
END { print("]"); } ' datafile.txt
datafile.txt包含,
Angela,Baker,2010-05-23
Betty,Crockett,1990-12-07
David,Done,2003-10-31
如前所述,JSON规范(基于ECMAScript 3)不允许尾随逗号。ES >= 5允许,所以你可以在纯JS中使用这个符号。这是有争议的,一些解析器确实支持它(http://bolinfest.com/essays/json.html, http://whereswalden.com/2010/09/08/spidermonkey-json-change-trailing-commas-no-longer-accepted/),但这是规范事实(如http://json.org/所示),它不应该在JSON中工作。那东西说……
... 我想知道为什么没有人指出你可以在第0次迭代时分割循环,并使用前导逗号而不是尾随逗号来摆脱比较代码的味道和循环中的任何实际性能开销,从而导致代码比其他提出的解决方案更短、更简单、更快(由于循环中没有分支/条件)。
例如(在c风格的伪代码中,类似于OP的建议代码):
s.append("[");
// MAX == 5 here. if it's constant, you can inline it below and get rid of the comparison
if ( MAX > 0 ) {
s.appendF("\"%d\"", 0); // 0-th iteration
for( int i = 1; i < MAX; ++i ) {
s.appendF(",\"%d\"", i); // i-th iteration
}
}
s.append("]");