Parse JSON with large numbers javascript

2019-02-19 11:37发布

问题:

I'm getting a JSON object that includes a long type value from an API response. Since javascript can't handle 64bit numbers, when executing JSON.parse on the response it rounds the number to the maximum value javascript can handle.

API response:

    {
       "subject": "subjectTitle",
       "longNumberKey": 7356270823847851521,
    }

When parsing this object to JSON, longNumberKey value would be 7356270823847852000 instead of 7356270823847851521. A solution would be to represent this value as a string but I can't control the API response for that.

回答1:

-----> json-bignum https://www.npmjs.com/package/json-bignum <------ JSON parser !

GMP (GNU multiple precision) with javascript https://github.com/jtobey/javascript-bignum/blob/master/src/gmp-example.html

gmp.js https://github.com/kripken/gmp.js?MobileOptOut=1