JSON 未正确转换长数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15869275/
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
JSON Not converting long numbers appropriately
提问by Jeet
I have a simple JSON where number is not getting parsed properly.
我有一个简单的 JSON,其中数字没有被正确解析。
[
{
"orderNumber": 1,
"customerId": 228930314431312345,
"shoppingCartId": 22893031443137109,
"firstName": "jjj"
}
]
I tried it @ http://www.utilities-online.info/xmltojson/and the result was
我在@ http://www.utilities-online.info/xmltojson/尝试过,结果是
<?xml version="1.0" encoding="UTF-8" ?>
<orderNumber>1</orderNumber>
<customerId>228930314431312350</customerId>
<shoppingCartId>22893031443137108</shoppingCartId>
<firstName>jjj</firstName>
As you can see....the XML is different from JSON. I'm new to JSON. Am I missing something?
如您所见....XML 与 JSON 不同。我是 JSON 的新手。我错过了什么吗?
采纳答案by Aiias
This is a Javascript precision problem.
这是一个 Javascript 精度问题。
According to Mozilla Developer Network:
根据 Mozilla 开发者网络:
ECMA-262 only requires a precision of up to 21 significant digits. Other implementations may not support precisions higher than required by the standard.
ECMA-262 只需要最多 21 位有效数字的精度。其他实现可能不支持高于标准要求的精度。
Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toPrecision
来源:https: //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toPrecision
I pasted your array into Google Chrome's Javascript console and got back this:

我将您的数组粘贴到 Google Chrome 的 Javascript 控制台中并得到了这个:

So it looks like Javascript is rounding the values before they are being converted to XML. Since your conversion is being done via Javascript in the browser at http://www.utilities-online.info/xmltojson/, it makes sense why the number was changed.
因此,看起来 Javascript 在将值转换为 XML 之前会对其进行四舍五入。由于您的转换是通过浏览器中的 Javascript 完成的,网址为http://www.utilities-online.info/xmltojson/,因此更改数字的原因是有道理的。
(Note: I tested on Google Chrome version 26.0.1410.43 m using Windows 7 Professional)
(注:我使用 Windows 7 Professional 在 Google Chrome 版本 26.0.1410.43 m 上进行了测试)
Edit:
编辑:
Is there any reason why you cannot pass these values to Javascript as strings?
有什么理由不能将这些值作为字符串传递给 Javascript?
Try this:
尝试这个:
[
{
"orderNumber": "1",
"customerId": "228930314431312345",
"shoppingCartId": "22893031443137109",
"firstName": "jjj"
}
]
I was able to do this and save the values successfully. However, you will not be able to run a math calculation on them in Javascript without losing some precision, unless you are doing something like multiplying by 0, of course.
我能够做到这一点并成功保存值。但是,您将无法在 Javascript 中对它们进行数学计算而不会损失一些精度,除非您正在执行乘以 0 之类的操作,当然。


This also converted to XML correctly using your reference http://www.utilities-online.info/xmltojson/.
这也使用您的参考http://www.utilities-online.info/xmltojson/正确转换为 XML 。

