4 ms·
Yes! I was shocked to see him recommend the use of a straight eval for parsing JSON. json2.js is a must! JSON.parse and JSON.stringify are your friends. https
by fletchowns 15y ago
Yes! I was shocked to see him recommend the use of a straight eval for parsing JSON.
json2.js is a must! JSON.parse and JSON.stringify are your friends.
https://github.com/douglascrockford/JSON-js/blob/master/json2.js https://github.com/douglascrockford/JSON-js/blob/master/json...
- olliej 15y agoToo my knowledge all browsers now ship with json built in -- if you must work with older browsers (not an entirely unreasonable requirement) you should check for the JSON object first, and if it isn't present load json2.js. Otherwise you're just adding an additional load penalty to your site (and on mobile browsers that can be 100s of milliseconds)
- DanielRibeiro 15y agoJson2.js does the check. However, it has to be loaded in order to be checked. Not a big issue if you are minifying and bundling all your js files before sending them to the browser client.
- daleharvey 15y agojust pointing out to others although the parent probably already knew, but JSON.parse is native in all modern browsers, json2 is only needed if you support older browsers (ie7)
- JackdawX 15y agoHoly crap, I did not know that.
- Encosia 15y agoKeep in mind that json2.js' JSON.parse() ultimately uses eval() after a few sanity checks. Definitely better than a blind eval(), but not quite the panacea it's often purported to be.