2013-04-25 95 views
-1

Hye ..對不起,如果這個問題已經問了。我試圖迭代通過使用JavaScript/jQuery的JSON數據。 JSON數據通過'$ .getJson'函數進行檢索。我的問題是JSON數據將具有無限的深度。對於簡短的問題感到抱歉。由於Jquery循環中的動態JSON數據

{ 
    "title": "perkhidmatan utama agensi", 
    "description": "", 
    "layout": "1", 
    "likerscale": "", 
    "elemen": [ 
     { 
      "title": "Sila tandakan dan nyatakan lokasi di mana Tuan/Puan menerima perkhidmatan. (Ibu Pejabat/Negeri/Cawangan)", 
      "elemen": [ 
       { 
        "title": "lokasi perkhidmatan", 
        "description": "", 
        "elemen": [ 
         { 
          "title": "ibu pejabat", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         }, 
         { 
          "title": "Negeri/Cawangan (Sila nyatakan)", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "1", 
          "textarea": "0", 
          "select": "0" 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "title": "Sila tandakan (<span class='icon icone-check'></span>) pada mana-mana ruangan berkenaan mengenai jenis perkhidmatan utama agensi ini yang pernah anda berurusan", 
      "elemen": [ 
       { 
        "title": "perkhidmatan utama JPN", 
        "description": "", 
        "elemen": [ 
         { 
          "title": "Kad Pengenalan", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         }, 
         { 
          "title": "Kelahiran, Kematian dan Anak Angkat", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         }, 
         { 
          "title": "Kewarganegaraan", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         }, 
         { 
          "title": "Perkahwinan dan Perceraian", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         }, 
         { 
          "title": "Lain-lain", 
          "checkbox": "1", 
          "radio": "0", 
          "input": "0", 
          "textarea": "0", 
          "select": "0" 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 
+0

你想在迭代做什麼呢? – 2013-04-25 13:09:36

+0

它被說了一百萬次,但你能提供代碼或你曾經嘗試過的任何東西嗎? – cgatian 2013-04-25 13:11:31

+0

可能重複的[我有一個嵌套的數據結構/ JSON,我如何訪問特定的值?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-我怎麼可以訪問一個特定的值) – 2013-04-25 13:15:43

回答

1

您這裏需要一個遞歸函數,這應該這樣做:

var data = yourJsonArray; 

function drillDownArray(data) { 
    // Main attributes 
    var title = data.title; 
    var description = data.description; 

    $.each(data.elemen, function(i, v) { 
     // Each element 
     var title = v.title; 
     drillDownArray(v); 
    } 
} 

drillDownArray(data); 
+0

感謝您的代碼。您的代碼在我的瀏覽器控制檯中返回錯誤 '未捕獲的類型錯誤:無法讀取未定義的屬性「長度」 – 2013-04-25 13:49:54

+0

您可以使用此代碼製作當前代碼的jsFiddle嗎?我會爲你解決它。 – 2013-04-25 13:50:53

+0

這裏是jsFiddle http://jsfiddle.net/H7hYt/ – 2013-04-25 14:07:54