使用 JSON 解析
- <!DOCTYPE html>
- <html>
- <body>
- <h1>用 JSON 字符串创建对象</h1>
- <p id="demo"></p>
- <script>
- var text = '{"employees":[' +
- '{"firstName":"Bill","lastName":"Gates" },' +
- '{"firstName":"Steve","lastName":"Jobs" },' +
- '{"firstName":"Elon","lastName":"Musk" }]}';
- obj = JSON.parse(text);
- document.getElementById("demo").innerHTML =
- obj.employees[1].firstName + " " + obj.employees[1].lastName;
- </script>
- </body>
- </html>