访问嵌套的 JSON 对象
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JSON 对象</h1>
- <p>如何访问嵌套的 JSON 对象。</p>
- <p id="demo"></p>
- <script>
- var myObj = {
- "name":"Bill Gates",
- "age":62,
- "cars": {
- "car1":"Porsche",
- "car2":"BMW",
- "car3":"Volvo"
- }
- }
- document.getElementById("demo").innerHTML += myObj.cars.car2 + "<br>";
- //or:
- document.getElementById("demo").innerHTML += myObj.cars["car2"];
- </script>
- </body>
- </html>