zoukankan      html  css  js  c++  java
  • Json数组

    1.语法

      [ "Google", "Runoob", "Taobao" ]、

      JSON 数组在中括号中书写。

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

      JavaScript 中,数组值可以是以上的 JSON 数据类型,也可以是 JavaScript 的表达式,包括函数,日期,及 undefined

    2.访问

     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 
    12         var myObj;
    13         myObj = {
    14             "name":"网站",
    15             "num":3,
    16             "sites":[ "Google", "Runoob", "Taobao" ]
    17         }
    18         
    19         document.getElementById("demo").innerHTML = myObj.sites[0];
    20 
    21     </script>
    22      
    23 </body>
    24 </html>

    效果

      

    3.循环

      

    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="demo"></p>
     9 
    10     <script>
    11 
    12         var myObj, i, j, x = "";
    13         myObj = {
    14             "name":"网站",
    15             "num":3,
    16             "sites": [
    17                 { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻译" ] },
    18                 { "name":"Runoob", "info":[ "菜鸟教程", "菜鸟工具", "菜鸟微信" ] },
    19                 { "name":"Taobao", "info":[ "淘宝", "网购" ] }
    20             ]
    21         }
    22 
    23         for (i in myObj.sites) {
    24             x += "<h1>" + myObj.sites[i].name + "</h1>";
    25             for (j in myObj.sites[i].info) {
    26                 x += myObj.sites[i].info[j] + "<br>";
    27             }
    28         }
    29 
    30         document.getElementById("demo").innerHTML = x;
    31 
    32 </script>
    33 </body>
    34 </html>

    效果:

      

    5.修改

      myObj.sites[1] = "Github";

    6.删除

      delete myObj.sites[1];

  • 相关阅读:
    rt_list_entry() 函数
    替换空格
    跳台阶
    斐波那契数列
    基于5221码的同步十进制加法计数器
    强连通分量+Tarjia+缩点
    次小生成树
    差分约束
    P1547 Out of Hay
    P1197 [JSOI2008]星球大战
  • 原文地址:https://www.cnblogs.com/juncaoit/p/9368810.html
Copyright © 2011-2022 走看看