访问嵌套的 JSON 数组
- <!DOCTYPE html>
- <html>
- <body>
-
- <h1>JSON 数组</h1>
-
- <p>遍历数组内的数组。</p>
-
- <p id="demo"></p>
-
- <script>
- var myObj, i, j, x = "";
- myObj = {
- "name":"Bill Gates",
- "age":62,
- "cars": [
- {"name":"Porsche", "models":["911", "Taycan"]},
- {"name":"BMW", "models":["M5", "M3", "X5"]},
- {"name":"Volvo", "models":["XC90", "V60"] }
- ]
- }
- for (i in myObj.cars) {
- x += "<h2>" + myObj.cars[i].name + "</h2>";
- for (j in myObj.cars[i].models) {
- x += myObj.cars[i].models[j] + "<br>";
- }
- }
- document.getElementById("demo").innerHTML = x;
- </script>
-
- </body>
- </html>