使用括号表示法修改值
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JSON 对象</h1>
- <p>如何使用括号表示法修改 JSON 对象中的值。</p>
- <p id="demo"></p>
- <script>
- var myObj, i, x = "";
- myObj = {
- "name":"Bill Gates",
- "age":62,
- "cars": {
- "car1":"Porsche",
- "car2":"BMW",
- "car3":"Volvo"
- }
- }
- myObj.cars["car2"] = "Mercedes Benz";
- for (i in myObj.cars) {
- x += myObj.cars[i] + "<br>";
- }
- document.getElementById("demo").innerHTML = x;
- </script>
- </body>
- </html>