原型 - evalJSON() 方法
此方法評估字串中的 JSON,並返回生成的物件。如果可選的 sanitize 引數設定為 true,則會檢查字串中是否存在惡意的嘗試,如果檢測到則不會呼叫 eval。
語法
string.evalJSON([sanitize = false]);
返回值
返回一個字串。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var str = '{ "name": "Violet", "occupation": "character" }';
var person = str.evalJSON();
alert( "Name :" + person.name);
alert( "Occupation :" + person.occupation);
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
輸出
prototype_string_processing.htm
廣告