使用点号表示法修改值

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JSON 对象</h1>
  6.  
  7. <p>如何修改 JSON 对象中的值。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var myObj, i, x = "";
  13. myObj = {
  14. "name":"Bill Gates",
  15. "age":62,
  16. "cars": {
  17. "car1":"Porsche",
  18. "car2":"BMW",
  19. "car3":"Volvo"
  20. }
  21. }
  22. myObj.cars.car2 = "Mercedes Benz";
  23.  
  24. for (i in myObj.cars) {
  25. x += myObj.cars[i] + "<br>";
  26. }
  27.  
  28. document.getElementById("demo").innerHTML = x;
  29. </script>
  30.  
  31. </body>
  32. </html>