来自服务器的Javascript长整数不正确 [英] Javascript long integer from server is not accurate
问题描述
我有一个 API ,我将其制成了get
请求获取数据.当我尝试保存Id
时,我看到Javascript的最后一位四舍五入,这使我的程序崩溃了!
I've got an API which I make a get
request to fetch data. When i try to save the Id
, I see Javascript round the last digit of it and it makes my program to break!
我看到了这个问题,但是如何保存每个 string
?
I see THIS QUESTION but how can I save each Id
as string
?
我正在使用全局数组存储所选项目的数据,因此,无论如何将JSON
的一个属性保存在string
中?
I'm using a global array to store the selected items' data so, anyway to save one attribute of a JSON
in string
?
我将拥有(例如)3个项目,并对每个Id
发出另一个get
请求:
I'm going to have (for example) 3 items and make another get
request for each Id
:
axios.get(`http://api.nemov.org/api/v1/Market/Symbol/${this.props.ID}`)
其中一个Id
是:9481703061634967
,但是JS将其转换为9481703061634968
,因此get
请求被破坏了!
One of those Id
s, is: 9481703061634967
but JS convert that to 9481703061634968
so the get
request is broken!
有解决方案吗?
推荐答案
在此问题上查看我的解决方案:>
See my solution on this question:
将响应转换为字符串,然后将带有正则表达式的替换项应用于 将ID字段转换为字符串类型:
Transform the response to string, then apply a repalce with a regex to convert Id field to string type:
const axios = require("axios");
axios.get(url, {transformResponse: [data => data]}).then((response) => {
let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'))
console.log(parsed)
});
这篇关于来自服务器的Javascript长整数不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!