javascript 所有主流浏览器都支持 JSON.parse 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4908875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is JSON.parse supported by all major browsers?
提问by Bryan Field
Possible Duplicate:
Browser-native JSON support (window.JSON)
Specifically, is JSON.parse(...)supported by IE7+, Firefox 2+, Chrome, Safari?
具体来说,是否JSON.parse(...)支持 IE7+、Firefox 2+、Chrome、Safari?
See: JSON in JavaScript
回答by T.J. Crowder
The answer in 2013 (and later)
2013 年(及以后)的答案
Is JSON.parse supported by all major browsers?
所有主流浏览器都支持 JSON.parse 吗?
Pretty much, yes (source). Even IE8 has it (provided you're not in IE7 emulation mode). If you need to support IE7 and earlier, read on.
差不多,是的(来源)。甚至 IE8 都有它(前提是您不在 IE7 仿真模式下)。如果您需要支持 IE7 及更早版本,请继续阅读。
The original answer from 2011
2011年的原始答案
No, older browsers (IE7 for instance) mostly don't have it. (More: http://caniuse.com/#search=JSON.parse)
不,较旧的浏览器(例如 IE7)大多没有它。(更多:http: //caniuse.com/#search=JSON.parse)
However, just a small script is all you need. The inventor of JSON, Douglas Crockford, has no fewer than three for you to choose from on his Github page:
但是,您只需要一个小脚本。JSON 的发明者 Douglas Crockford 在他的 Github 页面上有不少于三个供您选择:
json2.js: Provides bothJSON.parseandJSON.stringify. Parsing uses a few regexes to defend against script injection attacks and then passes the result toeval. This isn't generally considered a very good idea.json_parse.js: A recursive-descent parser that doesn't useeval.json_parse_state.js: A state-machine parser that doesn't useeval.
json2.js: 提供JSON.parse和JSON.stringify。解析使用一些正则表达式来防御脚本注入攻击,然后将结果传递给eval. 这通常不被认为是一个很好的主意。json_parse.js:一个不使用的递归下降解析器eval。json_parse_state.js:不使用eval.
Use what suits you. :-)
使用适合你的。:-)
Just about any major library (like jQuery, Prototype, YUI, Closure, or any of several others) will also provide JSON parsing, although in some cases it may well be a thin veneer on eval.
几乎任何主要库(如jQuery、Prototype、YUI、Closure或其他几个库中的任何一个)也将提供 JSON 解析,尽管在某些情况下它可能只是eval.
回答by Sarfraz
I am afraid not. You can however use json2 script written by Douglas Crockford.
恐怕没有。但是,您可以使用Douglas Crockford 编写的 json2 脚本。
Here is what John Resig (creator of jQuery) has to say about it:
以下是 John Resig(jQuery 的创建者)对此的评论:
JSON2.js - Late last year Crockford quietly released a new version of his JSON API that replaced his existing API. The important difference was that it used a single base object (JSON) instead of extending all native object prototypes (booo!).
JSON2.js - 去年年底,Crockford 悄悄发布了他的 JSON API 的新版本,取代了他现有的 API。重要的区别在于它使用单个基础对象 (JSON) 而不是扩展所有本机对象原型(嘘!)。
http://ejohn.org/blog/the-state-of-json/
http://ejohn.org/blog/the-state-of-json/

