所有主流浏览器都支持 JSON.parse 吗? [英] Is JSON.parse supported by all major browsers?
问题描述
可能的重复:
浏览器原生 JSON 支持 (window.JSON)
具体来说,IE7+、Firefox 2+、Chrome、Safari 是否支持 JSON.parse(...)
?
2013(及以后)的答案
<块引用>所有主流浏览器都支持 JSON.parse 吗?
差不多,是的(来源).甚至 IE8 都有它(前提是您不在 IE7 仿真模式下).如果您需要支持 IE7 及更早版本,请继续阅读.
2011 年的原始答案
不,较旧的浏览器(例如 IE7)大多没有它.(更多:http://caniuse.com/#search=JSON.parse)>
然而,您只需要一个小脚本.JSON 的发明者 Douglas Crockford 在 他的 Github 页面上有不少于三个供您选择.一个>:
json2.js
:同时提供JSON.parse
和JSON.stringify
.解析使用一些正则表达式来防御脚本注入攻击,然后将结果传递给eval
.这通常不是一个好主意.json_parse.js
:一个不使用eval
的递归下降解析器.json_parse_state.js
:不使用eval
的状态机解析器.
使用适合您的东西.:-)
几乎所有主要库(如 jQuery、原型、YUI、Closure 或 任何一个其他人) 也将提供 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屋!