javascript 十六进制格式可以用于 JSON 文件吗?如果是这样,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52671719/
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
Can hex format be used with JSON files? If so, how?
提问by Andrei Oniga
The following object is a valid one in plain Javascript. However, if the same is added to a JSON file, the file does not pass validation. Why is that?
以下对象是纯 Javascript 中的有效对象。但是,如果将相同的内容添加到 JSON 文件中,则该文件不会通过验证。这是为什么?
var message = {
"senderID": [ 0x01 ],
"receiverID": [ 0xFF ],
"commandCode": [ 0x00, 0x05 ],
"payload": [ 0xFF ]
}
回答by DESH
回答by chazsolo
The JSON spec supports numbersas values but explicitly does not support octal or hexidecimal. This is in part to increase interchange between languages. You could just as easily represent 0xFFas a string, "0xFF"and parse that out when using it.
JSON 规范支持数字作为值,但明确不支持八进制或十六进制。这部分是为了增加语言之间的交流。您可以像0xFF字符串一样轻松地表示,"0xFF"并在使用它时将其解析出来。
From json.org:
来自json.org:
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
数字与 C 或 Java 数字非常相似,只是不使用八进制和十六进制格式。
According to the ECMA-404 Final draft:
A number is a sequence of decimal digits with no superfluous leading zero. It may have a preceding minus sign (U+002D). It may have a fractional part prefixed by a decimal point (U+002E). It may have an exponent, prefixed by e (U+0065) or E (U+0045) and optionally + (U+002B) or – (U+002D). The digits are the code points U+0030 through U+0039.
数字是没有多余的前导零的十进制数字序列。它可能有一个前面的减号 (U+002D)。它可能有一个以小数点 (U+002E) 为前缀的小数部分。它可能有一个指数,前缀为 e (U+0065) 或 E (U+0045),可选 + (U+002B) 或 – (U+002D)。这些数字是代码点 U+0030 到 U+0039。

