从JSON数组获取值的问题

时间:2017-04-18 09:53:12

标签: php jquery json

我使用jQuery / AJAX从PHP文件中使用json_encode返回两个对象。响应如下:

{"result":{"trueresults":[["April 12th 2017, 1:05:52 pm","8734hjgfd784ew"],["April 12th 2017, 4:53:57 pm","8734hjgfd784ew"]],"falseresults":[["April 12th 2017, 1:07:03 pm","8734hjgfd784ew"],["April 12th 2017, 4:56:17 pm","8734hjgfd784ew"]]},"errors":false}

{"result":{"trueresults":[["April 12th 2017, 1:05:52 pm","17x1p14e6662"],["April 12th 2017, 1:05:54 pm","17x1p14e6662"]],"falseresults":[["April 12th 2017, 1:05:53 pm","17x1p14e6662"],["April 12th 2017, 1:05:55 pm","17x1p14e6662"]]},"errors":false}

然后我循环结果并做一些像这样的日期时间计算:

.success(function(response) {
    if(!response.errors && response.result) {
        var trueresultgroup = response.result.trueresults;
        var falseresultgroup = response.result.falseresults;
        var tot = moment.duration(0);
            for (i = 0; i < trueresultgroup.length; i++) {                  
              var on = (trueresultgroup[i]).toString();
              var off = (falseresultgroup[i]).toString();
              var ms = moment(off,"MMMM Do YYYY, h:mm:ss a").diff(moment(on,"MMMM Do YYYY, h:mm:ss a"));
              tot.add(ms, 'ms');                  
              var dtime = moment.duration(tot).asHours();
            }
            var s = tot.format("hh [hours] :mm [minutes] :ss [seconds]");
            $("#divtimes").append('<p>'+s+'</p>');

    } else {
        alert("error");
    }
}); 

如何访问数组中的第二个值?我可以从["April 12th 2017, 1:05:52 pm","8734hjgfd784ew"]访问“2017年4月12日,下午1:05:52”部分,如何访问“8734hjgfd784ew”?

为了澄清,我正在计算设备使用日期的时间。我能够做到这一点,但我想添加devicename(8734hjgfd784ew等),然后在它旁边添加结果,但我不知道如何在响应中访问该部分。

0 个答案:

没有答案