从Jquery JSON响应。ajax是四舍五入的,所以我不能得到正确的值

时间:2022-06-03 18:47:21

I am performing an AJAX get request and this is the response I get in a program like Paw/Postman:

我正在执行AJAX get请求,这是我在Paw/Postman这样的程序中得到的响应:

[
  {
    "registrantKey": 4131401026087862797,
  }
]

However when I do:

然而当我做:

.done(function(data, textStatus, jqXHR) {
    $.each(data, function(index){
    console.log(data[index].registrantKey);
    });
})

it is treating this registrant key value as a large number and is rounding off the last numbers so the value being printed to the console is: 4131401026087863000. How do I get the original value as shown in Paw/Postman. I don't need to perform any math operations on it I just need the value. I tried experimenting with the toString() method but I couldn't get this to work. Your help would be much appreciated.

它将这个注册表键值视为一个大数字,并将最后的数字四舍五入,因此打印到控制台的值为:4131401026087863000。我如何得到如Paw/Postman所示的原始值。我不需要对它进行任何数学运算我只需要这个值。我尝试使用toString()方法进行实验,但是我无法让它正常工作。非常感谢你的帮助。

update Unfortunately this just isn't possible in javascript. The only solution is to use another language or to try and get original developers to return value as a string. Thanks Rory and Madalin.

不幸的是,这在javascript中是不可能的。唯一的解决方案是使用另一种语言,或者尝试让原始开发人员以字符串的形式返回值。谢谢罗里和Madalin。

3 个解决方案

#1


1  

If you can access to your json as string and not as object (maybe in the jqXHR object), you can transform all numbers to string like this :

如果可以将json作为字符串访问,而不是作为对象访问(可能在jqXHR对象中),则可以将所有数字转换为如下所示的字符串:

jsonText = jsonText.replace(/([^\D]\d+[^\D])/g, "\"$1\"").replace(/""/g, "\"");

jsonText = jsonText.replace(/(^ \[D]\ D +[^ \ D])/ g,\“\”“1美元)。替换(/ / g," \ ");

(Be careful with my regex, i don't really sure if it works in any case)

(小心我的regex,我不确定它在任何情况下是否有效)

And after load your jsonText as an object.

将jsonText作为对象加载后。

#2


1  

The issue is due to the precision of JS and has nothing to do with jQuery or AJAX. In JS a number can only contain a maximum of 16 integers, up to a maximum of 9,007,199,254,740,991 (aka Number.MAX_SAFE_INTEGER)

问题在于JS的精度,与jQuery或AJAX无关。在JS中,一个数字最多只能包含16个整数,最多为9,007,199,254,740,991(也就是Number.MAX_SAFE_INTEGER)。

If you need to display a larger number then you would have to work with it as a string, but be aware that it will be rounded down to the max precision JS can use if you attempt to convert it to a Number type.

如果需要显示较大的数字,则必须将其作为字符串处理,但是要注意,如果您试图将其转换为数字类型,那么它将被四舍五入到可以使用的最大精度JS。

#3


0  

My first solution was: Send json string as text/plain from backend; With horroble regex replace numbers with strings then parse to object.

我的第一个解决方案是:从后端以文本/纯格式发送json字符串;用horroble regex用字符串替换数字,然后解析为对象。

Then after some bugs we modified json serializer(jackson lib) to write as string if value is instance of Number; Send string as application/json from backend; Removed frontend code since jquery does parsing if content type is json;

然后,在一些bug之后,我们修改了json序列化器(jackson lib),如果值是Number的实例,则将其写入字符串;从后端发送字符串作为应用程序/json;由于jquery在内容类型为json时进行解析,所以删除了前端代码;

Operating with big numbers is done by BigNumber JQ plugin

使用大数字的操作是由BigNumber JQ插件完成的

#1


1  

If you can access to your json as string and not as object (maybe in the jqXHR object), you can transform all numbers to string like this :

如果可以将json作为字符串访问,而不是作为对象访问(可能在jqXHR对象中),则可以将所有数字转换为如下所示的字符串:

jsonText = jsonText.replace(/([^\D]\d+[^\D])/g, "\"$1\"").replace(/""/g, "\"");

jsonText = jsonText.replace(/(^ \[D]\ D +[^ \ D])/ g,\“\”“1美元)。替换(/ / g," \ ");

(Be careful with my regex, i don't really sure if it works in any case)

(小心我的regex,我不确定它在任何情况下是否有效)

And after load your jsonText as an object.

将jsonText作为对象加载后。

#2


1  

The issue is due to the precision of JS and has nothing to do with jQuery or AJAX. In JS a number can only contain a maximum of 16 integers, up to a maximum of 9,007,199,254,740,991 (aka Number.MAX_SAFE_INTEGER)

问题在于JS的精度,与jQuery或AJAX无关。在JS中,一个数字最多只能包含16个整数,最多为9,007,199,254,740,991(也就是Number.MAX_SAFE_INTEGER)。

If you need to display a larger number then you would have to work with it as a string, but be aware that it will be rounded down to the max precision JS can use if you attempt to convert it to a Number type.

如果需要显示较大的数字,则必须将其作为字符串处理,但是要注意,如果您试图将其转换为数字类型,那么它将被四舍五入到可以使用的最大精度JS。

#3


0  

My first solution was: Send json string as text/plain from backend; With horroble regex replace numbers with strings then parse to object.

我的第一个解决方案是:从后端以文本/纯格式发送json字符串;用horroble regex用字符串替换数字,然后解析为对象。

Then after some bugs we modified json serializer(jackson lib) to write as string if value is instance of Number; Send string as application/json from backend; Removed frontend code since jquery does parsing if content type is json;

然后,在一些bug之后,我们修改了json序列化器(jackson lib),如果值是Number的实例,则将其写入字符串;从后端发送字符串作为应用程序/json;由于jquery在内容类型为json时进行解析,所以删除了前端代码;

Operating with big numbers is done by BigNumber JQ plugin

使用大数字的操作是由BigNumber JQ插件完成的