0

这是示例 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}

那么我怎样才能在最近的日期和时间达到同样的效果。请帮我。

4

2 回答 2

1

您可以简单地找到具有最大“日期”(毫秒)的记录:

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);
    }
}
于 2021-08-01T17:48:55.403 回答
1

方法一

跟踪循环本身中具有最大(最近)时间戳的记录。

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}

方法二

我们可以实现一个自定义ComparatorList<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}
于 2021-08-01T18:01:29.197 回答