2013-06-03 54 views
0

我有一個JSON對象,如下所示。通過具有相同名稱的子級的多級JSON對象循環

{ 
    "name":"Me", 
    "groups": [ 
     { 
      "name":"My Garage", 
      "groups":[ 
       { 
        "name":"My Cars", 
        "groups":[], 
        "tags":[ 
         { 
          "name":"Fiet Punto" 
         }, 
         { 
          "name":"Gallardo" 
         } 
        ] 
       }, 
       { 
        "name":"My Bikes", 
        "groups":[] 
       } 
      ] 
     }, 
     { 
      "name":"My Bank", 
      "groups":[ 
       { 
        "name":"Swiss Bank", 
        "groups":[] 
       }, 
       { 
        "name":"Bank of America", 
        "groups":[], 
        "tags":[ 
         { 
          "name":"Account 1" 
         }, 
         { 
          "name":"Account 2" 
         } 
        ] 
       } 
      ] 
     } 
    ], 
    "tags":[ 
     { 
      "name":"My tag 1" 
     }, 
     { 
      "name":"My tag 2" 
     } 
    ] 
} 

我的期望輸出如下:

Me 
--My Garage 
    --My Cars 
    --Fiet Punto 
    --Gallardo 
    --My Bikes 
--My Bank 
    --Swiss Bank 
    --Bank of America 
    -- Account 1 
    -- Account 2 
--My Tag 1 
--My Tag 2 

我在構建遞歸問題。我想要做的是:

  • 取根對象。
  • 打印name
  • 查找對象是否存在groupstags
  • 如果它們中的任何一個存在,則循環通過內部對象。
  • 應用正確的縮進並按照以上步驟操作內部對象。

我該如何做到這一點?

編輯:

function getAllGroups(jsonObject) 
{ 

    //print the name of the current level object. 
    $("body").append(jsonObject.name); 

    //now if this object contains groups 
    if(jsonObject.hasOwnProperty('groups')) 
    { 
     //and if it is an array 
     if(jsonObject.groups.length > 0) 
     { 
      //then for each object in groups of this object, call the function again. 
      $(jsonObject.groups).each(function(i, innerObject){ 
       //print the index for debugging. 
       console.log(i + innerObject.name); 
       //make a recursive call to the function. 
       getAllGroups(innerObject); 

      }); 

     } 

    } 
    else 
    { 
     console.log("does not exist anymore.") 
    } 
} 

我無法弄清楚如何試用並行兩種tagsgroups和打印維護級別的名稱。

更確切地說,我無法弄清楚如何獲得樹的級別。

+4

請發佈你迄今爲止嘗試過的代碼。你究竟遇到了什麼問題? –

回答

1

要獲得樹的水平,我認爲最簡單的辦法就是在目前的水平傳遞給方法:

function getAllGroups(jsonObject,currentlevel) 
{ 

    //print the name of the current level object. 
    $("body").append(jsonObject.name); 

    //now if this object contains groups 
    if(jsonObject.hasOwnProperty('groups')) 
    { 
     //and if it is an array 
     if(jsonObject.groups.length > 0) 
     { 
      var nextlevel = currentlevel+1; 
      //then for each object in groups of this object, call the function again. 
      $(jsonObject.groups).each(function(i, innerObject){ 
       //print the index for debugging. 
       console.log(i + innerObject.name); 
       //make a recursive call to the function. 
       getAllGroups(innerObject,nextlevel); 

      }); 

     } 

    } 
    else 
    { 
     console.log("does not exist anymore.") 
    } 
} 

通過啓動第一級使用它:

getAllGroups(yourJSON,1); 
+0

這樣做!但沒有幫助..沒有得到適當的水平 –

+1

@pjp:這個想法是隻傳遞當前水平的方法。你如何管理你的當前水平取決於你。上面的代碼只是一個例子。那麼,但我認爲它應該符合你的要求。你能否再次檢查是否有其他問題?希望你很快找到答案。 –

相關問題