所有主流浏览器都支持JSON.parse吗? [英] Is JSON.parse supported by all major browsers?
问题描述
可能重复:
浏览器本地JSON支持(window.JSON)
具体来说,是 JSON.parse(...)
是否由IE7 +,Firefox 2+,Chrome,Safari支持?
请参阅: JavaScript中的JSON
2013年(以后)的答案
所有主流浏览器都支持JSON.parse吗?
相当多,是的(源)。甚至IE8有它(如果你不是在IE7仿真模式)。如果您需要支持IE7及更早版本,请继续阅读。
2011年的原始答案
浏览器(例如IE7)大多没有它。 (详情: http://caniuse.com/#search=JSON.parse ) / p>
但是,只需要一个小脚本即可。 JSON的发明者Douglas Crockford有不少于三个,您可以从他的Github页面:
-
json2.js
:提供JSON.parse
和JSON.stringify
。解析使用几个正则表达式来防御脚本注入攻击,然后将结果传递给eval
。这通常不是一个很好的主意。 -
json_parse.js
:一个递归下降解析器, code> eval 。 -
json_parse_state.js
:状态机解析器, t使用eval
。
: - )
关于任何主要图书馆(例如 jQuery , Prototype , YUI ,关闭或任何其他几个人)也将提供JSON解析,虽然在某些情况下它可能是一个薄的单板 eval
。
Possible Duplicate:
Browser-native JSON support (window.JSON)
Specifically, is JSON.parse(...)
supported by IE7+, Firefox 2+, Chrome, Safari?
See: JSON in JavaScript
The answer in 2013 (and later)
Is JSON.parse supported by all major browsers?
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.
The original answer from 2011
No, older browsers (IE7 for instance) mostly don't have it. (More: 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:
json2.js
: Provides bothJSON.parse
andJSON.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
.
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
.
这篇关于所有主流浏览器都支持JSON.parse吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!