1. 程式人生 > >json和陣列的巢狀練習

json和陣列的巢狀練習

---恢復內容開始---

今天學習了json和陣列的巢狀,例如var a={wenke:"文科",like:["數學","物理","化學"]},

"wenke","like"為a的key,類似陣列的下標,可以自定義,也方便儲存一些資訊,其中a["like"]為一個數組,當我們想輸入“數學”的時候,可以這樣取出:

alert ["like"][0];

做個複雜一點的例題:

    設計一個json 將下面所有資訊儲存起來,有三個年級,每個年級三個班,每個班有三個學生,學生資訊如下:

小紅  女  15

家屬:紅1     女   16

  紅2 女   17

  紅3 女   18

 

  小明  男  16

家屬:明1     男   17

  明2 男   18

  明3 男   19

 

  小剛  男  30

家屬:剛1  男  31

  剛2  男  32

  剛3  男  33

var a =

     {

        一年級:

        {

          一班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

     

二班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

          三班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            }

        },

二年級:

        {

          一班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

          二班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

          三班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            }

        },

三年級:

        {

          一班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

          二班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            },

          三班:

          {

          xiaohong:["小紅","女",15,[["紅1","女",16],["紅2","女",17],["紅3","女",18]]],

          xiaoming:["小明","男",16,[["明1","男",17],["明2","男",18],["明3","男",19]]],

          xiaogang:["小剛","男",30,[["剛1","男",31],["剛2","男",32],["剛3","男",33]]]

            }

        }

     }

----------------------------------------------------------------------------------------------

//如果我們要取出一年級二班小剛的家屬剛1的年齡

console.log(a["一年級"]["二班"]["xiaogang"][3][0][2]);