4 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果使用,您將更容易受到攻擊eval
:JSON是Javascript的子集,而json.parse僅解析JSON,而eval
這卻為所有JS表達(dá)式敞開了大門。

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
所有JSON.parse實(shí)現(xiàn)最有可能使用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 + ')');
的優(yōu)點(diǎn)JSON.parse是它可以驗(yàn)證參數(shù)是否為正確的JSON語法。

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
JSON.parse()和eval()接受的內(nèi)容有所不同。嘗試評(píng)估:
var x =“ {\” shoppingCartName \“:\” shopping_cart:2000 \“}”
eval(x) //won't work
JSON.parse(x) //does work
添加回答
舉報(bào)