JavaScript基础-03-json解析

示例

  • 代码
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":"[email protected]" },' +
	'{ "name":"张飞" , "email":"[email protected]" },' +
	'{ "name":"赵云" , "emaill":"[email protected]" } ]}';
	
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.persons[1].name + ": " + obj.persons[1].email;
script>

body>
html>

getElementById:根据指定的 id 属性值得到对象

  • 结果输出
    JavaScript基础-03-json解析_第1张图片

你可能感兴趣的:(#,JavaScript基础,JavaScript,前端,json解析,基础教程)