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

前端 未结 4 1082
走了就别回头了
走了就别回头了 2021-01-20 00:31

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:

[{
\"         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 00:49

    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 welcome.

提交回复
热议问题