如何根据 JSON 数组 - JAVA 中最近的日期时间过滤数据?
How to filter the data according to recent datetime in JSON Array - JAVA?
这是示例 JSON:
{
"items": [
{
"id": "3655",
"address": "+911234567890",
"date": "1627831097120",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo 2",
"serviceCenter": "+911234567890"
},
{
"id": "3654",
"address": "+911234567890",
"date": "1627830900372",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo1",
"serviceCenter": "+911234567890"
},
{
"id": "3654",
"address": "+910000000000",
"date": "1627831097460",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo1",
"serviceCenter": "+9110000000000"
}
]
}
我正在使用 Jayway JsonPath 查询数据,下面是示例代码。
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
for (Map<String, Object> resultMap : results) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
Instant instant = Instant.ofEpochMilli(milliSeconds);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
if (zonedDateTime.getLong(ChronoField.MINUTE_OF_HOUR) <= 3) {
System.out.println("Message received within 3 minutes \t" + resultMap);
} else {
System.out.println("Message received before 3 minutes \t" + resultMap);
}
}
使用上面给出的代码,我过滤了 3 分钟内收到的记录。但有时我会收到多条记录
结果:
Message received before 3 minutes {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}
Message received before 3 minutes {id=3654, address=+911234567890, person=null, date=1627830900372, read=0, status=-1, type=1, subject=null, body=Demo1, serviceCenter=+911234567890}
我要最近的记录
Message received before 3 minutes {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}
那么我怎样才能在最近的日期和时间之前达到同样的效果。请帮助我。
您可以简单地找到具有最大“日期”(毫秒)的记录:
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
Map<String, Object> recentResultMap = null;
long maxTime = 0L;
for (Map<String, Object> resultMap : results) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
if( milliSeconds > maxTime ){
maxTime = milliSeconds;
recentResultMap = resultMap;
}
}
然后打印记录:
if( recentResultMap != null ){
long milliSeconds = Long.parseLong((String) recentResultMap.get("date"));
Instant instant = Instant.ofEpochMilli(milliSeconds);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
if (zonedDateTime.getLong(ChronoField.MINUTE_OF_HOUR) <= 3) {
System.out.println("Message received within 3 minutes \t" + recentResultMap);
} else {
System.out.println("Message received before 3 minutes \t" + recentResultMap);
}
}
方法一
跟踪循环本身中具有最大(最近)时间戳的记录。
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
Map<String, Object> mostRecentTimestampItem = new HashMap<>();
long mostRecentTimestamp = 0;
for (Map<String, Object> resultMap : d) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
if (mostRecentTimestamp < milliSeconds) {
mostRecentTimestamp = milliSeconds;
mostRecentTimestampItem = resultMap;
}
}
System.out.println("Most recent record = " + mostRecentTimestampItem);
以上代码片段的输出是:-
Most recent record = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}
方法二
我们可以实现一个自定义 Comparator
,按“日期”值的降序对 List<Map<String, Object>>
进行排序。最近的记录将是已排序 List
.
中的第一条记录
List<Map<String, Object>> resultMapsList = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
System.out.println("ORIGINAL MAPS LIST = " + resultMapsList);
Collections.sort(resultMapsList, (o1, o2) -> Long.parseLong(o1.get("date").toString()) < Long.parseLong(o2.get("date").toString()) ? 1 : -1);
System.out.println("SORTED MAPS LIST = " + resultMapsList);
System.out.println("MOST RECENT RECORD = " + resultMapsList.get(0));
以上代码片段的输出是:-
ORIGINAL MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
SORTED MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
MOST RECENT RECORD = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}
这是示例 JSON:
{
"items": [
{
"id": "3655",
"address": "+911234567890",
"date": "1627831097120",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo 2",
"serviceCenter": "+911234567890"
},
{
"id": "3654",
"address": "+911234567890",
"date": "1627830900372",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo1",
"serviceCenter": "+911234567890"
},
{
"id": "3654",
"address": "+910000000000",
"date": "1627831097460",
"read": "0",
"status": "-1",
"type": "1",
"body": "Demo1",
"serviceCenter": "+9110000000000"
}
]
}
我正在使用 Jayway JsonPath 查询数据,下面是示例代码。
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
for (Map<String, Object> resultMap : results) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
Instant instant = Instant.ofEpochMilli(milliSeconds);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
if (zonedDateTime.getLong(ChronoField.MINUTE_OF_HOUR) <= 3) {
System.out.println("Message received within 3 minutes \t" + resultMap);
} else {
System.out.println("Message received before 3 minutes \t" + resultMap);
}
}
使用上面给出的代码,我过滤了 3 分钟内收到的记录。但有时我会收到多条记录
结果:
Message received before 3 minutes {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}
Message received before 3 minutes {id=3654, address=+911234567890, person=null, date=1627830900372, read=0, status=-1, type=1, subject=null, body=Demo1, serviceCenter=+911234567890}
我要最近的记录
Message received before 3 minutes {id=3655, address=+911234567890, person=null, date=1627831097120, read=0, status=-1, type=1, subject=null, body=Demo 2, serviceCenter=+911234567890}
那么我怎样才能在最近的日期和时间之前达到同样的效果。请帮助我。
您可以简单地找到具有最大“日期”(毫秒)的记录:
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
Map<String, Object> recentResultMap = null;
long maxTime = 0L;
for (Map<String, Object> resultMap : results) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
if( milliSeconds > maxTime ){
maxTime = milliSeconds;
recentResultMap = resultMap;
}
}
然后打印记录:
if( recentResultMap != null ){
long milliSeconds = Long.parseLong((String) recentResultMap.get("date"));
Instant instant = Instant.ofEpochMilli(milliSeconds);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
if (zonedDateTime.getLong(ChronoField.MINUTE_OF_HOUR) <= 3) {
System.out.println("Message received within 3 minutes \t" + recentResultMap);
} else {
System.out.println("Message received before 3 minutes \t" + recentResultMap);
}
}
方法一
跟踪循环本身中具有最大(最近)时间戳的记录。
List<Map<String, Object>> d = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
Map<String, Object> mostRecentTimestampItem = new HashMap<>();
long mostRecentTimestamp = 0;
for (Map<String, Object> resultMap : d) {
long milliSeconds = Long.parseLong((String) resultMap.get("date"));
if (mostRecentTimestamp < milliSeconds) {
mostRecentTimestamp = milliSeconds;
mostRecentTimestampItem = resultMap;
}
}
System.out.println("Most recent record = " + mostRecentTimestampItem);
以上代码片段的输出是:-
Most recent record = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}
方法二
我们可以实现一个自定义 Comparator
,按“日期”值的降序对 List<Map<String, Object>>
进行排序。最近的记录将是已排序 List
.
List<Map<String, Object>> resultMapsList = JsonPath.parse(strJSON).read("$.items[?(@.address=='+911234567890')]");
System.out.println("ORIGINAL MAPS LIST = " + resultMapsList);
Collections.sort(resultMapsList, (o1, o2) -> Long.parseLong(o1.get("date").toString()) < Long.parseLong(o2.get("date").toString()) ? 1 : -1);
System.out.println("SORTED MAPS LIST = " + resultMapsList);
System.out.println("MOST RECENT RECORD = " + resultMapsList.get(0));
以上代码片段的输出是:-
ORIGINAL MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
SORTED MAPS LIST = [{"id":"3655","address":"+911234567890","date":"1627831097120","read":"0","status":"-1","type":"1","body":"Demo 2","serviceCenter":"+911234567890"},{"id":"3654","address":"+911234567890","date":"1627830900372","read":"0","status":"-1","type":"1","body":"Demo1","serviceCenter":"+911234567890"}]
MOST RECENT RECORD = {id=3655, address=+911234567890, date=1627831097120, read=0, status=-1, type=1, body=Demo 2, serviceCenter=+911234567890}