本文介绍了在javascript中解析json - 长数字被舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个包含长数字的json(在java servlet中生成)。问题是长数字被舍入。

I need to parse a json that contains a long number (that was produces in a java servlet). The problem is the long number gets rounded.

执行此代码时:

var s = '{"x":6855337641038665531}';
var obj = JSON.parse(s);
alert (obj.x);

输出为:

6855337641038666000

在此处查看示例:

为什么会这样,怎么能我解决了吗?

why is that, and how can I solve it?

推荐答案

正如其他人所说,这是因为这个数字太大了。但是,您可以通过将数字作为字符串发送来解决此限制:

As others have stated, this is because the number is too big. However, you can work around this limitation by sending the number as a string like so:

var s = '{"x":"6855337641038665531"}';

然后您可以使用诸如使用该号码。

Then instead of using JSON.parse(), you can use a library such as javascript-bignum to work with the number.

这篇关于在javascript中解析json - 长数字被舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:13