这是示例 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}
那么我怎样才能在最近的日期和时间达到同样的效果。请帮我。