如果整数以前导零开头,为什么 JSON 无效? [英] Why is JSON invalid if an integer begins with a leading zero?

查看:47
本文介绍了如果整数以前导零开头,为什么 JSON 无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 JSON 文件导入我的 Parse.com 项目,但我不断收到错误无效的键:值对".

I'm importing some JSON files into my Parse.com project, and I keep getting the error "invalid key:value pair".

它表明有一个意外的8".

It states that there is an unexpected "8".

这是我的 JSON 示例:

Here's an example of my JSON:

}
 "Manufacturer":"Manufacturer",
 "Model":"THIS IS A STRING",
 "Description":"",
 "ItemNumber":"Number12345",
 "UPC":083456789012,
 "Cost":"$0.00",
 "DealerPrice":" $0.00 ",
 "MSRP":" $0.00 ",
}

如果我通过从 "UPC":083456789012, 中删除 0 或将其转换为 "UPC":"083456789012", 它变得有效.

If I update the JSON by either removing the 0 from "UPC":083456789012, or converting it to "UPC":"083456789012", it becomes valid.

JSON 真的不能接受以 0 开头的整数吗,或者有没有办法解决这个问题?

Can JSON really not accept an integer that begins with 0, or is there a way around the problem?

推荐答案

JavaScript 中前导 0 表示八进制数.八进制数不能包含 8;因此,该数字无效.此外,JSON 不(正式)支持八进制数,因此正式的 JSON 是无效的,即使该数字不包含 8.不过,一些解析器确实支持它,这可能会导致一些混淆.其他解析器会将其识别为无效序列并抛出错误,尽管它们给出的确切解释可能有所不同.

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.

解决方案:如果您有一个数字,永远不要用前导零存储它.如果您有一个需要前导零的值,请不要将其视为数字,而是将其视为字符串.用引号将其存储起来.

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.

在这种情况下,您有一个 UPC,它需要 12 位数字并且可能包含前导零.我认为最好的存储方式是将其存储为字符串.

In this case, you've got a UPC which needs to be 12 digits long and may contain leading zeroes. I think the best way to store it is as a string.

不过,这是有争议的.如果您将其视为条形码,将前导 0 视为其组成部分,那么字符串就有意义了.其他类型的条形码甚至可以包含字母字符.

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.

另一方面.UPC 是一个数字,它的左填充有 0 到 12 位数字这一事实可以被视为一种显示属性.实际上,如果您通过添加额外的 0 将其左填充为 13 位,您就得到了一个 EAN 代码,因为 EAN 是 UPC 的超集.

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.

如果您有货币金额,您可以将其显示为 € 7.30,而将其存储为 7.3,因此存储产品代码也是有意义的作为一个数字.

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.

但是这个决定取决于你.我只能建议你使用字符串,这是我个人对这些代码的偏好,如果你选择一个数字,那么你必须删除 0 以使其工作.

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 0 to make it work.

这篇关于如果整数以前导零开头,为什么 JSON 无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆