2016-03-25 67 views
-2

我對返回JSON數據的服務進行了API調用。如果我使用以下方式記錄數據:JSON.parse(response.body);解析JavaScript中的JSON數據

這會記錄:

{ Results: 
    [ { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:28:22Z', 
     MonitorType: 0, 
     NodeName: 'fmsHealthMonitorAppId01', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:30:15Z', 
     MonitorType: 1, 
     NodeName: 'fmsHealthMonitorAppId01', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:30:15Z', 
     MonitorType: 2, 
     NodeName: 'fmsHealthMonitorAppId01', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:28:49Z', 
     MonitorType: 0, 
     NodeName: 'fmsHealthMonitorAppId02', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:35:23Z', 
     MonitorType: 1, 
     NodeName: 'fmsHealthMonitorAppId02', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2016-03-25T21:35:23Z', 
     MonitorType: 2, 
     NodeName: 'fmsHealthMonitorAppId02', 
     Status: 0 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2014-11-03T16:20:15Z', 
     MonitorType: 0, 
     NodeName: 'fmsHealthMonitorAppId03', 
     Status: 2 }, 
    { Interval: 900, 
     IsNodeActive: true, 
     LastOccurrence: '2013-08-30T21:44:41Z', 
     MonitorType: 0, 
     NodeName: 'fmsHealthMonitorAppId04', 
     Status: 2 }, 
    { Interval: 900, 
     IsNodeActive: false, 
     LastOccurrence: '2014-11-03T16:34:45Z', 
     MonitorType: 1, 
     NodeName: 'fmsHealthMonitorAppId03', 
     Status: 1 }, 
    { Interval: 900, 
     IsNodeActive: false, 
     LastOccurrence: '2014-11-03T16:34:45Z', 
     MonitorType: 2, 
     NodeName: 'fmsHealthMonitorAppId03', 
     Status: 1 }, 
    { Interval: 900, 
     IsNodeActive: false, 
     LastOccurrence: '2013-08-30T21:57:58Z', 
     MonitorType: 1, 
     NodeName: 'fmsHealthMonitorAppId04', 
     Status: 1 }, 
    { Interval: 900, 
     IsNodeActive: false, 
     LastOccurrence: '2013-08-30T21:57:58Z', 
     MonitorType: 2, 
     NodeName: 'fmsHealthMonitorAppId04', 
     Status: 1 } ] } 

然後如果我這樣做:

var resp = JSON.parse(response.body); 
var totalStatuses = Object.keys(resp.Results).length; 

totalStatuses包含12個正確的計數。

現在我想遍歷數據的每個部分並檢查LastOccurrence的值。這是逃避我。正確的語法是什麼來做到這一點?

回答

2

不要打擾使用Object.keys,這只是如果你想獲得一個對象的關鍵。你有什麼是一個數組。有了這個,你可以使用簡單的for循環遍歷它。

for (var i = 0; i < resp.Results.length; i++) { 
    var result = resp.Results[i]; 
    console.log(result.LastOccurrence); 
} 
+0

完美邁克!一個繼續。我需要通過調用Date.parse()將result.LastOccurrence轉換爲毫秒。我試過: var last_time = Date.parse(result.LastOccurrence);或者將toString()添加到最後,但是這兩個都給了一個TypeError,說undefined不是一個函數。怎麼做才能做到這一點? – Bob

+0

@Bob'Date.parse'會將它轉換爲毫秒。 'Date.parse('2016-03-25T21:28:22Z')=== 1458941302000'。或者,您可以創建一個Date對象,然後使用getTime。 'new Date(result.LastOccurrence).getTime()'。 –

0

這必須工作

for(var i = 0; i < totalStatuses ; i += 1){ 
    var obj = resp[i]; 
    console.log(obj.LastOccurrence); 
} 
0

resp.Results是對象的數組,但Object.keys函數需要它的參數是一個對象。
要通過resp.Results數組遍歷:

var results = resp.Results, len = resp.Results.length; 
while (len--) { 
    console.log(result[len].LastOccurrence); 
}