没有前导零的小数是否有效JSON? [英] Are decimals without leading zeros valid JSON?
问题描述
鉴于JSON文件
{"percentageAmount": .01}
由JSONLint.com运行会导致错误:
Running it by JSONLint.com results in the error:
Parse error on line 2:
..."percentageAmount": .01}
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
另一方面,这是有效的:
On the other hand, this is valid:
{"percentageAmount": 0.01}
如果将代码作为JavaScript文字分配给变量,代码将被正确解析,但当然很多东西都适用于非JSON规范的JavaScript变量。
The code is parsed correctly if assigned to a variable as a JavaScript literal, but of course there are many things that are OK for JavaScript variables that aren't JSON spec.
为什么这是针对JSON规范的呢?
Why is this against JSON spec?
推荐答案
没有前导零的小数是否有效JSON?
Are decimals without leading zeros valid JSON?
从规范:
number = [ minus ] int [ frac ] [ exp ]
decimal-point = %x2E ; .
digit1-9 = %x31-39 ; 1-9
e = %x65 / %x45 ; e E
exp = e [ minus / plus ] 1*DIGIT
frac = decimal-point 1*DIGIT
int = zero / ( digit1-9 *DIGIT )
minus = %x2D ; -
plus = %x2B ; +
zero = %x30 ; 0
强制数字的唯一部分是 int
定义为零
或 1-9
后跟任意位数。
The only part of a number that is mandatory is int
which is defined as zero
or 1-9
followed by any number of digits.
所以JSON Lint是正确的。
So JSON Lint is correct.
为什么这是针对JSON规范的?
Why is this against JSON spec?
据我所知,作者以这种方式定义它的原因没有在任何地方记录。
As far as I know, the author's reasons for defining it that way are not documented anywhere.
这篇关于没有前导零的小数是否有效JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!