Whats the best JSON JavaScript polyfill

2020-05-05 17:41发布

问题:

I am looking for a JSON polyfill (for JSON support in older browsers) that I can use in some JavaScript I'm writing. I've looked and found JSON2 and JSON3 are quite popular and I've read that JSON3 is meant to be a drop in replacement for JSON2 but I was wondering if these are the best polyfills out there?

The only problem I have with JSON3 is that when I run the google closure lint checks over the JSON3 library it complains about for loops not defining a body: If this if/for/while really shouldn't have a body, use {}

Should the fact that the js-lint is showing warnings put me off using this polyfill as when I compile my library it makes it look like my library has these problems as I'm packaging it up together to simplify it for people downloading my javascript.

回答1:

You can use the JSON library provided by Douglas Crockford.

https://github.com/douglascrockford/JSON-js.

You can include it unconditionally, and it adds JSON.parse and JSON.stringify only if there isn't one defined yet.