使用 JSON 解析

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>用 JSON 字符串创建对象</h1>
  6.  
  7. <p id="demo"></p>
  8.  
  9. <script>
  10. var text = '{"employees":[' +
  11. '{"firstName":"Bill","lastName":"Gates" },' +
  12. '{"firstName":"Steve","lastName":"Jobs" },' +
  13. '{"firstName":"Elon","lastName":"Musk" }]}';
  14.  
  15. obj = JSON.parse(text);
  16. document.getElementById("demo").innerHTML =
  17. obj.employees[1].firstName + " " + obj.employees[1].lastName;
  18. </script>
  19.  
  20. </body>
  21. </html>