【代码笔记】Web-JavaScript-JavaScript JSON

一,效果图。

二,代码。

复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>JavaScript JSON</title>
</head>

<body>
    <h2>为JSON字符串创建对象</h2>
    <p id="demo"></p>
    <script>
    var text = '{"employees":[' +
        '{"firstName":"John","lastName":"Doe" },' +
        '{"firstName":"Anna","lastName":"Smith" },' +
        '{"firstName":"Peter","lastName":"Jones" }]}';

    obj = JSON.parse(text);
    document.getElementById("demo").innerHTML = obj.employees[1].firstName + " " + obj.employees[1].lastName;
    </script>
</body>

</html>
复制代码

 

参考资料:《菜鸟教程》

 

posted @ 2019-01-08 09:21  花儿迎风笑  阅读(230)  评论(0编辑  收藏  举报