我對返回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的值。這是逃避我。正確的語法是什麼來做到這一點?
完美邁克!一個繼續。我需要通過調用Date.parse()將result.LastOccurrence轉換爲毫秒。我試過: var last_time = Date.parse(result.LastOccurrence);或者將toString()添加到最後,但是這兩個都給了一個TypeError,說undefined不是一個函數。怎麼做才能做到這一點? – Bob
@Bob'Date.parse'會將它轉換爲毫秒。 'Date.parse('2016-03-25T21:28:22Z')=== 1458941302000'。或者,您可以創建一個Date對象,然後使用getTime。 'new Date(result.LastOccurrence).getTime()'。 –