如果整数以前导零开头,为什么 JSON 无效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27361565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is JSON invalid if an integer begins with a leading zero?
提问by DJSrA
I'm importing some JSON files into my Parse.com project, and I keep getting the error "invalid key:value pair".
我正在将一些 JSON 文件导入我的 Parse.com 项目,但我不断收到错误“无效的键:值对”。
It states that there is an unexpected "8".
它指出有一个意外的“8”。
Here's an example of my JSON:
这是我的 JSON 示例:
}
"Manufacturer":"Manufacturer",
"Model":"THIS IS A STRING",
"Description":"",
"ItemNumber":"Number12345",
"UPC":083456789012,
"Cost":""UPC":"083456789012"
.00",
"DealerPrice":" JSONNumber ::
-_opt DecimalIntegerLiteral JSONFraction_opt ExponentPart_opt
.00 ",
"MSRP":" DecimalIntegerLiteral ::
0
NonZeroDigit DecimalDigits_opt
.00 ",
}
If I update the JSON by either removing the 0from "UPC":083456789012,or converting it to "UPC":"083456789012",it becomes valid.
如果我通过删除更新JSON0从"UPC":083456789012,或将其转换成"UPC":"083456789012",它变得有效。
Can JSON really not accept an integer that begins with 0, or is there a way around the problem?
JSON 真的不能接受以 开头的整数0,还是有办法解决这个问题?
回答by GolezTrol
A leading 0 indicates an octal number in JavaScript. An octal number cannot contain an 8; therefore, that number is invalid. Moreover, JSON doesn't (officially) support octal numbers, so formally the JSON is invalid, even if the number would not contain an 8. Some parsers do support it though, which may lead to some confusion. Other parsers will recognize it as an invalid sequence and will throw an error, although the exact explanation they give may differ.
前导 0 表示 JavaScript 中的八进制数。八进制数不能包含 8;因此,该数字无效。此外,JSON 不(正式)支持八进制数,因此正式的 JSON 是无效的,即使该数字不包含 8。不过,一些解析器确实支持它,这可能会导致一些混淆。其他解析器会将其识别为无效序列并抛出错误,尽管它们给出的确切解释可能有所不同。
Solution:If you have a number, don't ever store it with leading zeroes. If you have a value that needs to have a leading zero, don't treat it as a number, but as a string. Store it with quotes around it.
解决方案:如果你有一个数字,永远不要用前导零存储它。如果您有一个需要前导零的值,请不要将其视为数字,而是将其视为字符串。用引号将其存储起来。
In this case, you've got a UPC which needs to be 12 digits longand may contain leading zeroes. I think the best way to store it is as a string.
在这种情况下,您有一个 UPC,它需要 12 位长并且可能包含前导零。我认为存储它的最好方法是作为一个字符串。
It is debatable, though. If you treat it as a barcode, seeing the leading 0 as an integral part of it, then string makes sense. Other types of barcodes can even contain alphabetic characters.
不过,这是有争议的。如果将其视为条形码,将前导 0 视为其组成部分,那么字符串就有意义了。其他类型的条形码甚至可以包含字母字符。
On the other hand. A UPC is a number, and the fact that it's left-padded with zeroes to 12 digits could be seen as a display property. Actually, if you left-pad it to 13 digits by adding an extra 0, you've got an EAN code, because EAN is a superset of UPC.
另一方面。UPC 是一个数字,它左填充零到 12 位数字这一事实可以被视为一种显示属性。实际上,如果您通过添加额外的 0 将其左填充为 13 位,您就得到了一个 EAN 代码,因为 EAN 是 UPC 的超集。
If you have a monetary amount, you might display it as 7.30, while you store it as 7.3, so it could also make sense to store a product code as a number.
如果您有一个货币金额,您可以将其显示为7.30,而将其存储为7.3,因此将产品代码存储为数字也是有意义的。
But that decision is up to you. I can only advice you to use a string, which is my personal preference for these codes, and if you choose a number, then you'll have to remove the 0to make it work.
但这个决定取决于你。我只能建议您使用字符串,这是我个人对这些代码的偏好,如果您选择一个数字,那么您必须删除0以使其工作。
回答by Quentin
One of the more confusing parts of JavaScript is that if a number starts with a 0that isn't immediately followed by a ., it represents an octal, not a decimal.
JavaScript 中更令人困惑的部分之一是,如果一个数字以 a 开头,0后面没有紧跟 a .,则它代表八进制,而不是十进制。
JSON borrows from JavaScript syntax but avoids confusing features, so simply bans numbers with leading zeros (unless then are followed by a .) outright.
JSON 借鉴了 JavaScript 语法,但避免了混淆的功能,因此直接禁止带有前导零的数字(除非后面跟着 a .)。
Even if this wasn't the case, there would be no reason to expect the 0to still be in the number when it was parsed since 02and 2are just difference representations of the same number (if you force decimal).
即使这是不是这样,也就没有理由期望0仍处于数量时,这是自解析02和2有相同数量的只是差表示(如果强制十进制)。
If the leading zero is important to your data, then you probably have a string and not a number.
如果前导零对您的数据很重要,那么您可能有一个字符串而不是数字。
HexIntegerLiteral ::
0x HexDigit
0X HexDigit
HexIntegerLiteral HexDigit
A product codeis an identifier, not something you do maths with. It should be a string.
一个产品代码是一个标识符,不是你做数学的东西。它应该是一个字符串。
回答by lexicore
Formally, it is because JSON uses DecimalIntegerLiteralin its JSONNumberproduction:
正式地,这是因为 JSONDecimalIntegerLiteral在其JSONNumber生产中使用:
OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit
And DecimalIntegerLiteralmay only start with 0if it is 0:
并DecimalIntegerLiteral可能只有开始0,如果它是0:
var a = 065; //Octal Value
var b = 53; //Decimal Value
a == b; //true
The rationale behind is is probably:
背后的理由大概是:
- In the JSON Grammar - to reuse constructs from the main ECMAScript grammar.
- In the main ECMAScript grammar - to make it easier to distinguish
DecimalIntegerLiteralfromHexIntegerLiteralandOctalIntegerLiteral.OctalIntegerLiteralin the first place.
- 在 JSON 语法中 - 重用来自主要 ECMAScript 语法的构造。
- 在主要的ECMAScript语法-更容易区分
DecimalIntegerLiteral从HexIntegerLiteral和OctalIntegerLiteral。OctalIntegerLiteral首先。
See this productions:
看这个作品:
##代码##...
...
##代码##回答by causita
The UPC should be in string format. For the future you may also get other type of UPC such as GS128 or string based product identification codes. Set your DB column to be string.
UPC 应为字符串格式。将来,您可能还会获得其他类型的 UPC,例如 GS128 或基于字符串的产品识别代码。将您的数据库列设置为字符串。
回答by Alireza MH
I think the easiest way to send your number by JSON is send your number as string.
我认为通过 JSON 发送您的号码的最简单方法是将您的号码作为字符串发送。
回答by tantalum
If an integer start with 0in JavaScript it is considered to be the Octal (base 8) value of the integer instead of the decimal (base 10) value. For example:
如果整数以0JavaScript开头,则它被认为是整数的八进制(以 8 为基数)值,而不是十进制(以 10 为基数)值。例如:

