原型 - 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
广告