3 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
JavaScript中最令人困惑的部分之一是,如果數(shù)字以a開頭0,而不是緊隨其后的.,則它表示一個(gè)八進(jìn)制,而不是十進(jìn)制。
JSON借用了JavaScript語法,但避免了混淆的功能,因此完全禁止使用帶前導(dǎo)零的數(shù)字(除非后面帶有a .)。
即使這是不是這樣,也就沒有理由期望0仍處于數(shù)量時(shí),這是自解析02和2有相同數(shù)量的只是差表示(如果強(qiáng)制十進(jìn)制)。
如果前導(dǎo)零對(duì)您的數(shù)據(jù)很重要,則可能是字符串而不是數(shù)字。
"UPC":"083456789012"
一個(gè)產(chǎn)品代碼是一個(gè)標(biāo)識(shí)符,不是你做數(shù)學(xué)的東西。應(yīng)該是一個(gè)字符串。

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
正式地,這是因?yàn)镴SON DecimalIntegerLiteral在其JSONNumber生產(chǎn)中使用:
JSONNumber ::
-_opt DecimalIntegerLiteral JSONFraction_opt ExponentPart_opt
并DecimalIntegerLiteral可能只有開始0,如果它是0:
DecimalIntegerLiteral ::
0
NonZeroDigit DecimalDigits_opt
背后的原因可能是:
在JSON語法中-重用ECMAScript主語法中的構(gòu)造。
在主要的ECMAScript語法-更容易區(qū)分DecimalIntegerLiteral從HexIntegerLiteral和OctalIntegerLiteral。OctalIntegerLiteral首先。
看到這個(gè)作品:
HexIntegerLiteral ::
0x HexDigit
0X HexDigit
HexIntegerLiteral HexDigit
...
OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit
添加回答
舉報(bào)