示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>玄德公记事</title>
</head>
<body>
<h2>为 JSON 字符串创建对象</h2>
<p id="demo"></p>
<script>
var text = '{ "persons" : [' +
'{ "name":"关羽" , "email":"guanyu@xishu.com" },' +
'{ "name":"张飞" , "email":"zhangfei@xishu.com" },' +
'{ "name":"赵云" , "emaill":"zhaoyun@xishu.com" } ]}';
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.persons[1].name + ": " + obj.persons[1].email;
</script>
</body>
</html>
getElementById
:根据指定的 id 属性值得到对象
- 结果输出