来自服务器的Javascript长整数不正确 [英] Javascript long integer from server is not accurate

查看:134
本文介绍了来自服务器的Javascript长整数不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 Ids, 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屋!

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