【问题标题】:Filter json response accordingly to time stamp in kotlin根据 kotlin 中的时间戳过滤 json 响应
【发布时间】:2021-11-22 12:53:34
【问题描述】:

嘿,我收到了来自 retrofit 的 json 响应。我想根据 sentAt 属性过滤。我想比较 最新时间 并相应地移动块。现在在我的 json 中有 4 个项目/块,但可能更多。有人可以知道如何以有效的方式过滤列表和修改。我还需要将 sentAt 值转换为 timedate 格式并存储在其他变量中。今天日期的格式将是 time,如果日期是旧的,我需要 date 格式。

Json 响应

{
  "conversations": [
    {
      "id": "789",
      "title": "Conversation Title 3",
      "lastMessage": {
        "id": "4677",
        "text": "Text 3",
        "sentAt": "2021-09-28T10:39:10.0492422+01:00"
      }
    },
    {
      "id": "456",
      "title": "Conversation Title 2",
      "lastMessage": {
        "id": "3c7e",
        "text": "Text2",
        "sentAt": "2021-09-26T12:39:10.0493518+01:00"
      }
    },
    {
      "id": "101",
      "title": "Conversation Title 4",
      "lastMessage": {
        "id": "f983",
        "text": "Text 4",
        "sentAt": "2021-09-30T13:39:10.0493537+01:00"
      }
    },
    {
      "id": "123",
      "title": "Conversation Title 1",
      "lastMessage": {
        "id": "f983",
        "text": "Text 1",
        "sentAt": "2021-09-26T12:38:00.0493537+01:00"
      }
    }......
  ]
}

预期结果

{
  "conversations": [
    {
      "id": "101",
      "title": "Conversation Title 4",
      "lastMessage": {
        "id": "f983",
        "text": "Text 4",
        "sentAt": "2021-09-30T13:39:10.0493537+01:00"
      }
    },
    {
      "id": "789",
      "title": "Conversation Title 3",
      "lastMessage": {
        "id": "4677",
        "text": "Text 3",
        "sentAt": "2021-09-28T10:39:10.0492422+01:00"
      }
    },
    {
      "id": "456",
      "title": "Conversation Title 2",
      "lastMessage": {
        "id": "3c7e",
        "text": "Text2",
        "sentAt": "2021-09-26T12:39:10.0493518+01:00"
      }
    },
    {
      "id": "123",
      "title": "Conversation Title 1",
      "lastMessage": {
        "id": "f983",
        "text": "Text 1",
        "sentAt": "2021-09-26T12:38:00.0493537+01:00"
      }
    }......
  ]
}

例如将值存储在变量中

id : 101
time : 13:39

id : 789
time : 28/09/2021

添加数据类

对话响应

data class ConversationsResponse(
    val conversations: List<Conversations>? = null
)

对话

data class Conversations(
    val id: String? = null,
    val title: String? = null,
    val lastMessage: LastMessage? = null
)

最后一条消息

data class LastMessage(
    val id: String? = null,
    val text: String? = null,
    val sentAt: String? = null
)

【问题讨论】:

  • 你是如何在 Java 中处理这个响应的?您使用 Gson、Jackson 或任何其他库还是手动解析它?我会使用java.time.OffsetDateTime 存储完整的日期、时间和偏移量,因为它实际上由LocalDateLocalTimeZoneOffset 组成,所有这些都可以单独访问。
  • @deHaar 我正在使用 moshi 库来解析 json 响应。以及如何过滤 json 响应?
  • 好的,你能告诉我们你正在使用的类吗?
  • @deHaar 你的意思是数据类?
  • 是的,数据类 (Kotlin) 或 Pojo (Java)

标签: java android date kotlin calendar


【解决方案1】:

这是一个解析输入时间并分别处理日期和时间部分的示例,为此我从您的 JSON 示例中提取了 sentAts 之一:

import java.time.LocalDate
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

fun main() {
    // example input from JSON
    val sentAt = "2021-09-28T10:39:10.0492422+01:00"
    // parse it directly, it is in ISO standard format
    val odt: OffsetDateTime = OffsetDateTime.parse(sentAt)
    // extract the date only
    val date: LocalDate = odt.toLocalDate()
    // extract the time of day
    val time: LocalTime = odt.toLocalTime()
    // format the date using a standard formatter
    val dateString = date.format(DateTimeFormatter.ISO_LOCAL_DATE)
    // and do the same for the time
    val timeString = time.format(DateTimeFormatter.ISO_LOCAL_TIME)
    // then print the formatted date and time separately
    println("date: $dateString and time: $timeString")
}

这个输出是

date: 2021-09-28 and time: 10:39:10.0492422

您可以直接在data class LastMessage 中使用它,也可以创建一些实用程序类来将Strings 中的日期时间转换为LastMessages。

可以使用不同的DateTimeFormatters 更改输出。有几个内置的,您可以使用 DateTimeFormatterBuilderDateTimeFormatter.ofPattern(String pattern) 创建自定义格式化程序。如果您想控制命名单位(如月份、星期几)的语言,可以将 Locale 传递给 DateTimeFormatter

【讨论】:

  • 感谢您的帮助。过滤json响应是我的第一要务,然后更改sentAt是我的第二要务。
  • OffsetDateTimeLocalDateLocalTime 是可比的,您可以先将 String 转换为 OffsetDateTime,然后再对它们进行排序。来自java.time 的那些对象便于过滤。
  • 我会尝试对它们进行排序
  • 很好的答案!值得一提的是,现代日期时间 API 基于 ISO 8601,只要日期时间字符串符合 ISO 8601 标准,就不需要明确使用 DateTimeFormatter 对象。
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
相关资源
最近更新 更多