我正在尝试按日期在JSON中解析数据并遇到问题。
基本上,我想抓住两个日期/时间之间的事件,但实际上没有运气。我知道我可以使用JSON解析器,但我发现的大多数解析器都会提取一个字段。
我不想提取字段 - 我想要在特定日期之后的所有事件的原始JSON。
因此,在下面的数据中,我想抓住2017-01-14凌晨2点之后的所有事件(每个JSON条目的所有4行)。我怎么能做到这一点?
{
"_id": "58816d03e4b00654468d2781",
"datetime": "2017-01-03T05:23:02Z",
"msg": "foo1"
"msg2": "foo2"
},
{
"_id": "58816d03e4b00654468d2963",
"datetime": "2017-01-14T01:50:52Z",
"msg": "foo1"
"msg2": "foo2"
},
{
"_id": "58816d03e4b00654468d3068",
"datetime": "2017-01-16T13:41:46Z",
"msg": "foo1"
"msg2": "foo2"
},
{
"_id": "58816d03e4b00654468d3068",
"datetime": "2017-01-20T21:16:40Z",
"msg": "foo1"
"msg2": "foo2"
},
答案 0 :(得分:0)
如果你的'event'对象在一个数组中,你可以使用array.filter()方法返回所有通过测试的对象(在你的情况下,如果datetime prop是在一段时间之后)
var filteredData,
testDate = new Date("2017-01-14T02:00:00Z");
function httpGetAsync(theUrl, callback){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
function filterResponseByDate(data){
filteredData = data.filter(function(d){
return new Date(d.datetime) > testDate;
});
}
httpGetAsync(yourDataURL,filterResponseByDate);
使用示例http get data for data
function Get-ScriptDirectory {
if ($psise) {Split-Path $psise.CurrentFile.FullPath}
else {$global:PSScriptRoot}
}