native JSON API 是 ECMAScript 3.1 里的新标准,目前
Firefox 3.1 和
IE 8 RC1 已经支持这个API。
这是浏览器内置的,所以解析json的速度会更快。同时保证了安全性。 在我的 Firefox 3.1 中测试如下代码: <script> var jsonstring = '{"name":"axis", "org":"ph4nt0m", "blog":"http://hi.baidu.com/aullik5"}'; var ok = JSON.parse(jsonstring); // 浏览器内置的API alert(ok.blog); </script> 上面这段代码在 IE7 里是通不过的。 用firebug 查看DOM,发现成功 parse 了 ![]() 使用内置的API,按照mozilla的说法,比使用eval来parse快了将近2.7倍 而且更安全,因为函数不会被解析,只会取里面的属性。原文这么说的: Native JSON parsing in Firefox 3.1 is safer because it does not support objects with functions. Attempting to convert an object with functions into a JSON string will only convert its properties and not its functions. And any malformed JSON string will result in a parse error instead of possible code execution. resource: http://blog.mozilla.com/webdev/2009/02/12/native-json-in-firefox-31/ |