2014-08-27 66 views
0

我正在使用dojo的arrayUtil.forEach來遍歷我的JSON對象,但是當我到達data.graphs.metrics時,它不會繼續,因爲度量不是數組。我有什麼選擇?JSON格式鑽取問題

xhr("/esp/files/eclwatch/ganglia.json", { 
    handleAs: "json" 
}).then(function(data){ 
     arrayUtil.forEach(data.graphs, function (item, idx) { 
     //never gets here. 
     }); 
    }); 
} 




//json file 
{ 
    "graphs": [ 
    { 
    "name": "Bar", 
    "metrics":{ 
     "metric": ["metric1, metric3"], 
     "metric": ["metric1", "metric4", "metric5"] 
    }, 
    "time": ["Hour", "Month", "Week"] 
    } 
    ] 
} 
+0

'metrics'是一個對象,它包含兩個同名的鍵。無論如何,你的'1/4/5'條目將覆蓋'1/3'條目。 – 2014-08-27 21:04:08

回答

0

使它成爲一個數組而不是給它重複鍵將是最有意義的。

"metrics": [ 
    ["metric1, metric3"], 
    ["metric1", "metric4", "metric5"] 
], 
+0

太棒了!你讓我的生活變得如此輕鬆。我正在做'度量標準':[metric:[「metric1」,「metric2」]],但JSONlint上的JSON無效。謝謝@Quentin – pcproff 2014-08-27 21:07:36