2013-12-11 243 views
0

我有這樣讀嵌套對象

var JSON_Object = { 
"Diabites": [ 
    { 
     "Day1": [ 
      { 
       "Breakfast": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Lunch": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Dinner": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      } 
     ] 
    }, 
    { 
     "Day2": [ 
      { 
       "Breakfast": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Lunch": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Dinner": [ 
        { 
         "food1": "Bread", 
         "food2": "Tea", 
         "food3": "Edd" 
        } 
       ] 
      } 
     ] 
    } 
] 
} 

對象現在,我怎麼能使用一些循環打印出來閱讀本文件。我嘗試以下

alert(JSON_Object.Diabites.length); // Return 1 
alert(JSON_Object.Diabites[0].Day1.length); // Return 3 

但也有不同的名稱,如Day1, Day2, Day3 ....,所以我怎麼能更改部分的循環,就像喜歡

for(var i=0; i<JSON_Object.Diabites.length;i++) 
{ 
    alert(JSON_Object.Diabites[i].Day1.length); 
} 

更新JSON對象

var JSON_Object = { 
"Diabites": [ 
    { 
     "Day1": [ 
      { 
       "Breakfast": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Lunch": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Dinner": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      } 
     ] 
    }, 
    { 
     "Day2": [ 
      { 
       "Breakfast": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Lunch": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      }, 
      { 
       "Dinner": [ 
        { 
         "food1": "Bread" 
        }, 
        { 
         "food2": "Tea" 
        }, 
        { 
         "food3": "Edd" 
        } 
       ] 
      } 
     ] 
    } 
] 
}; 
+0

[訪問/進程(嵌套)對象,數組或JSON的可能重複](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json)和[如何做我列舉了一個JavaScript對象的屬性?](http://stackoverflow.com/q/85992/218196)。 –

+1

請注意,該問題有**沒有**與JSON完全相關。這是關於如何在JavaScript中處理數組/對象。 *您如何獲得數據(例如,通過JSON)與問題無關。 –

回答

1

如果你想循環一個JSON的鍵,你可以這樣做:

for(var key in JSON_Object.Diabites[0]){ 

    //key will be Day1,Day2 etc. 
    alert(JSON_Object.Diabites[0][key].length); 

} 

但在共享您的JSON結構,你必須使用

for(var i=0;i<JSON_Object.Diabites.length;i++) { 

    for(var key in JSON_Object.Diabites[i]) { 

     //key will be Day1,Day2 etc. 
     alert(key); 
     alert(JSON_Object.Diabites[i][key].length); 

    } 

} 

here is a working fiddle如果你有興趣。

+0

嘿,這是非常有用的,但我在我的json對象之前犯了一個小錯誤,現在我更新了我的問題,你可以再看看嗎? – Blu