Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中JSON数据的解析_Javascript_Json - Fatal编程技术网

javascript中JSON数据的解析

javascript中JSON数据的解析,javascript,json,Javascript,Json,我对返回JSON数据的服务进行了API调用。如果我使用:JSON.parse(response.body)记录数据 这将被记录: { Results: [ { Interval: 900, IsNodeActive: true, LastOccurrence: '2016-03-25T21:28:22Z', MonitorType: 0, NodeName: 'fmsHealthMonitorAppId01', Sta

我对返回JSON数据的服务进行了API调用。如果我使用:JSON.parse(response.body)记录数据

这将被记录:

{ 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;
totalstatus
包含12个正确的计数


现在,我想遍历数据的每个部分,并检查LastOccurrence的值。这让我难以理解。正确的语法应该是什么?

不要麻烦使用
Object.keys
,这只是在您想要获取对象的键时。这里有一个数组。这样,您就可以使用一个简单的
for
循环对其进行迭代

for (var i = 0; i < resp.Results.length; i++) {
  var result = resp.Results[i];
  console.log(result.LastOccurrence);
}
for(var i=0;i
这必须起作用

for(var i = 0; i < totalStatuses ; i += 1){ 
    var obj = resp[i];
    console.log(obj.LastOccurrence);
}
for(var i=0;i
resp.Results
是一个对象数组,但
Object.keys
函数要求其参数为对象
要遍历
相应结果
数组,请执行以下操作:

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

完美的迈克!一个后续的。我需要通过调用Date.parse()将result.LastOccurrence转换为毫秒。我尝试了:var last_time=Date.parse(result.LastOccurrence);或者将toString()添加到末尾,但它们都给出了类型错误,表示undefined不是函数。该怎么做?@Bob
Date.parse
会将其转换为毫秒<代码>日期解析('2016-03-25T21:28:22Z')==1458941302000
。或者,您可以创建一个
Date
对象,然后使用
getTime
<代码>新日期(result.LastOccurrence).getTime()。