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

前端 未结 4 1080
走了就别回头了
走了就别回头了 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:43

    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"
    }
    

提交回复
热议问题