所有主流浏览器都支持JSON.parse吗? [英] Is JSON.parse supported by all major browsers?

查看:137
本文介绍了所有主流浏览器都支持JSON.parse吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

浏览器本地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 both JSON.parse and JSON.stringify. Parsing uses a few regexes to defend against script injection attacks and then passes the result to eval. This isn't generally considered a very good idea.
  • json_parse.js: A recursive-descent parser that doesn't use eval.
  • json_parse_state.js: A state-machine parser that doesn't use 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.

这篇关于所有主流浏览器都支持JSON.parse吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆