最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript - convert an EXTRA LARGE Number to string in JSON before the default parsing - Stack Overflow

programmeradmin0浏览0评论

As I mentioned in THIS QUESTION, I have problem when getting the response from the server.

I receive an array of objects with these attributes:

[{
"Id": 143187001116603,   // VERY big number which I want to convert it to string
"Name": "تملی612",   // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96",   // string
"InsCode": "IRO6MELZ96C1"   // string
},
...
]

Any simple way to convert a specified type (Number) to string in a JSON file?

I see the replacer argument in JSON.stringify() but I have no idea how to use that...

UPDATE

One of those Ids, is: 9481703061634967 but JS convert that to 9481703061634968!

UPDATE

As Jonas H said in this answer, JS run the default parsing and that's why i lose my real Id value!

Any idea?

As I mentioned in THIS QUESTION, I have problem when getting the response from the server.

I receive an array of objects with these attributes:

[{
"Id": 143187001116603,   // VERY big number which I want to convert it to string
"Name": "تملی612",   // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96",   // string
"InsCode": "IRO6MELZ96C1"   // string
},
...
]

Any simple way to convert a specified type (Number) to string in a JSON file?

I see the replacer argument in JSON.stringify() but I have no idea how to use that...

UPDATE

One of those Ids, is: 9481703061634967 but JS convert that to 9481703061634968!

UPDATE

As Jonas H said in this answer, JS run the default parsing and that's why i lose my real Id value!

Any idea?

Share Improve this question edited Oct 27, 2018 at 14:17 MoKhajavi75 asked Oct 27, 2018 at 13:28 MoKhajavi75MoKhajavi75 2,7125 gold badges31 silver badges57 bronze badges 1
  • JSON.parse(response) ? – DedaDev Commented Oct 27, 2018 at 13:29
Add a ment  | 

4 Answers 4

Reset to default 5

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);
});

Assuming that you receive the data as a Json string with the numbers inside them, there is no way to preserve the data using JSON.parse. Even if you use the second argument to add a transformation function, it will only be run after the default parsing has parsed the numbers with a loss of information in case of large numbers. You need to manipulate the string directly to wrap the number in quotes using e.g. a regular expression.

You can also use the json-bigint npm package: https://www.npmjs./package/json-bigint

you can use replacer in JSON.stringify() like :

var obj  = {
"Id": 143187001116603,   // VERY big number which I want to convert it to string
"Name": "تملی612",   // string
"Title": "تسهیلات مسکن بانک ملی-اسفند96",   // string
"InsCode": "IRO6MELZ96C1"   // string
};


function replacer(name, val) {
    // convert Number to string
    if ( val && val.constructor === Number ) {
        return val.toString();
    } else {
        return val; // return as is
    }
};

 JSON.stringify(obj, replacer, 4);

// result

{
    "Id": "143187001116603",
    "Name": "تملی612",
    "Title": "تسهیلات مسکن بانک ملی-اسفند96",
    "InsCode": "IRO6MELZ96C1"
}

function replacer(key, value) {
  // Filtering out properties
  if (key === 'Id') {
    return value.toString();
  }
  return value;
}

const t = [{
    "Id": 143187001116603, // VERY big number which I want to convert it to string
    "Name": "تملی612", // string
    "Title": "تسهیلات مسکن بانک ملی-اسفند96", // string
    "InsCode": "IRO6MELZ96C1" // string
  },
  {
    "Id": 9481703061634967, // VERY big number which I want to convert it to string
    "Name": "تملی232", // string
    "Title": "تسهیلات مسکن بانک ملی-اسفن216", // string
    "InsCode": "IRO6MSDZ96C1" // string
  }
]
const stringifiedValue = JSON.stringify(t, replacer)
console.log(JSON.parse(stringifiedValue))

Try this using replacer callback for JSON.stringify.

Feedbacks wele.

发布评论

评论列表(0)

  1. 暂无评论