猿问

JSON.parse与eval()

我的Spider Sense警告我,使用eval()解析传入的JSON是一个坏主意。我只是想知道JSON.parse()-我假设它是JavaScript的一部分,而不是浏览器特定的功能-是否更安全。



湖上湖
浏览 426回答 4
4回答

收到一只叮咚

如果使用,您将更容易受到攻击eval:JSON是Javascript的子集,而json.parse仅解析JSON,而eval这却为所有JS表达式敞开了大门。

青春有我

所有JSON.parse实现最有可能使用eval()JSON.parse基于Douglas Crockford的解决方案,该解决方案eval()在497行使用。// In the third stage we use the eval function to compile the text into a// JavaScript structure. The '{' operator is subject to a syntactic ambiguity// in JavaScript: it can begin a block or an object literal. We wrap the text// in parens to eliminate the ambiguity.j = eval('(' + text + ')');的优点JSON.parse是它可以验证参数是否为正确的JSON语法。

手掌心

JSON.parse()和eval()接受的内容有所不同。尝试评估:var x =“ {\” shoppingCartName \“:\” shopping_cart:2000 \“}”eval(x)         //won't workJSON.parse(x)   //does work
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答