此方法计算字符串中的 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>作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8886.html
支付宝
微信