zoukankan      html  css  js  c++  java
  • Json对象

    1.语法

      { "name":"runoob", "alexa":10000, "site":null }

      JSON 对象使用在大括号({})中书写。

      对象可以包含多个 key/value(键/值)对。

      key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。

      key 和 value 中使用冒号(:)分割。

      每个 key/value 对使用逗号(,)分割。

    2.访问

      有两种方式可以访问。

      第一种,使用( . )

        

      第二种,使用[]

        

    3.循环

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4     <meta  http-equiv="Content-Type" content="text/html; charset=gbk">
     5 <title>教程</title>
     6 </head>
     7 <body>
     8     <p id="demo"></p>
     9 
    10     <script>
    11         var myObj = { "name":"runoob", "alexa":10000, "site":null };
    12         for (x in myObj) {
    13             document.getElementById("demo").innerHTML += myObj[x] + "<br>";
    14         }
    15     </script>
    16      
    17 </body>
    18 </html>

    效果:

      

    4.嵌套

      两种写法,就是获取值的两种方式。

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4     <meta  http-equiv="Content-Type" content="text/html; charset=gbk">
     5 <title>教程</title>
     6 </head>
     7 <body>
     8     <p id="demo1"></p>
     9     <p id="demo2"></p>
    10 
    11     <script>
    12         myObj = {
    13             "name":"runoob",
    14             "alexa":10000,
    15             "sites": {
    16                 "site1":"www.runoob.com",
    17                 "site2":"m.runoob.com",
    18                 "site3":"c.runoob.com"
    19             }
    20         }
    21         document.getElementById("demo1").innerHTML = myObj.sites.site1 + "<br>";
    22         // 或者
    23         document.getElementById("demo2").innerHTML = myObj.sites["site1"];
    24     </script>
    25      
    26 </body>
    27 </html>

    效果:

      

    5.修改值

      同样是两种方式。

        myObj.sites.site1 = "www.google.com";

      或者:

        myObj.sites["site1"] = "www.google.com";

    6.删除对象属性

      我们可以使用 delete 关键字来删除 JSON 对象的属性。

        delete myObj.sites.site1;

      或者

        delete myObj.sites["site1"]

  • 相关阅读:
    第三周java学习总结
    第一周Java学习总结
    关闭窗体
    乱七八糟
    网页游戏资料
    timer控件的使用
    spread 签套循环改变颜色编号为颜色
    限制输入‘号的代码
    SQlcharindex命令
    限制输入类型
  • 原文地址:https://www.cnblogs.com/juncaoit/p/9368705.html
Copyright © 2011-2022 走看看