此方法使用正则表达式检查字符串是否为有效的 JSON。此安全方法在内部调用。
语法
string.isJSON();
返回值
返回一个布尔值。
例子
HTML
x
29
29
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
var str = "something";
9
alert("something.isJSON() : " + str.isJSON());
10
11
var str = "\"something\"";
12
alert("\"something\" : " + str.isJSON());
13
14
var str = "{ foo: 42 }";
15
alert("{ foo: 42 } : " + str.isJSON());
16
17
var str = "{ \"foo\": 42 }";
18
alert("{ \"foo\": 42 } : " + str.isJSON());
19
}
20
</script>
21
</head>
22
23
<body>
24
<p>Click the button to see the result.</p>
25
<br />
26
<br />
27
<input type = "button" value = "Result" onclick = "showResult();"/>
28
</body>
29
</html>
阅读剩余 80%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8865.html