• <bdo id='QARNx'></bdo><ul id='QARNx'></ul>

    <legend id='QARNx'><style id='QARNx'><dir id='QARNx'><q id='QARNx'></q></dir></style></legend><tfoot id='QARNx'></tfoot>
    1. <i id='QARNx'><tr id='QARNx'><dt id='QARNx'><q id='QARNx'><span id='QARNx'><b id='QARNx'><form id='QARNx'><ins id='QARNx'></ins><ul id='QARNx'></ul><sub id='QARNx'></sub></form><legend id='QARNx'></legend><bdo id='QARNx'><pre id='QARNx'><center id='QARNx'></center></pre></bdo></b><th id='QARNx'></th></span></q></dt></tr></i><div id='QARNx'><tfoot id='QARNx'></tfoot><dl id='QARNx'><fieldset id='QARNx'></fieldset></dl></div>

        <small id='QARNx'></small><noframes id='QARNx'>

      1. 我如何反序列化以杰克逊为单位的时间戳?

        时间:2023-07-27
        <legend id='OQfVp'><style id='OQfVp'><dir id='OQfVp'><q id='OQfVp'></q></dir></style></legend>
        • <bdo id='OQfVp'></bdo><ul id='OQfVp'></ul>
        • <tfoot id='OQfVp'></tfoot>

                    <tbody id='OQfVp'></tbody>

                  <small id='OQfVp'></small><noframes id='OQfVp'>

                • <i id='OQfVp'><tr id='OQfVp'><dt id='OQfVp'><q id='OQfVp'><span id='OQfVp'><b id='OQfVp'><form id='OQfVp'><ins id='OQfVp'></ins><ul id='OQfVp'></ul><sub id='OQfVp'></sub></form><legend id='OQfVp'></legend><bdo id='OQfVp'><pre id='OQfVp'><center id='OQfVp'></center></pre></bdo></b><th id='OQfVp'></th></span></q></dt></tr></i><div id='OQfVp'><tfoot id='OQfVp'></tfoot><dl id='OQfVp'><fieldset id='OQfVp'></fieldset></dl></div>
                  本文介绍了我如何反序列化以杰克逊为单位的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一些以秒为单位的时间戳(即 Unix 时间戳)的 JSON:

                  I've got some JSON that has timestamps in seconds (i.e. a Unix timestamp):

                  {"foo":"bar","timestamp":1386280997}
                  

                  要求 Jackson 将其反序列化为具有 DateTime 字段的对象以获取时间戳,结果为 1970-01-17T01:11:25.983Z,这是纪元之后不久的时间,因为 Jackson 假设它毫秒.除了撕开 JSON 并添加一些零之外,我如何让 Jackson 理解 seconds 时间戳?

                  Asking Jackson to deserialize this into an object with a DateTime field for the timestamp results in 1970-01-17T01:11:25.983Z, a time shortly after the epoch because Jackson is assuming it to be in milliseconds. Aside from ripping apart the JSON and adding some zeros, how might I get Jackson to understand the seconds timestamp?

                  推荐答案

                  我写了一个自定义deserializer 以秒为单位处理时间戳(Groovy 语法).

                  I wrote a custom deserializer to handle timestamps in seconds (Groovy syntax).

                  class UnixTimestampDeserializer extends JsonDeserializer<DateTime> {
                      Logger logger = LoggerFactory.getLogger(UnixTimestampDeserializer.class)
                  
                      @Override
                      DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                          String timestamp = jp.getText().trim()
                  
                          try {
                              return new DateTime(Long.valueOf(timestamp + '000'))
                          } catch (NumberFormatException e) {
                              logger.warn('Unable to deserialize timestamp: ' + timestamp, e)
                              return null
                          }
                      }
                  }
                  

                  然后我注释了我的 POGO 以将其用作时间戳:

                  And then I annotated my POGO to use that for the timestamp:

                  class TimestampThing {
                      @JsonDeserialize(using = UnixTimestampDeserializer.class)
                      DateTime timestamp
                  
                      @JsonCreator
                      public TimestampThing(@JsonProperty('timestamp') DateTime timestamp) {
                          this.timestamp = timestamp
                      }
                  }
                  

                  这篇关于我如何反序列化以杰克逊为单位的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java 时间戳 - 如何创建日期为 23/09/2007 的时间戳 下一篇:在 Java 中格式化时间戳

                  相关文章

                  最新文章

                • <i id='toirG'><tr id='toirG'><dt id='toirG'><q id='toirG'><span id='toirG'><b id='toirG'><form id='toirG'><ins id='toirG'></ins><ul id='toirG'></ul><sub id='toirG'></sub></form><legend id='toirG'></legend><bdo id='toirG'><pre id='toirG'><center id='toirG'></center></pre></bdo></b><th id='toirG'></th></span></q></dt></tr></i><div id='toirG'><tfoot id='toirG'></tfoot><dl id='toirG'><fieldset id='toirG'></fieldset></dl></div>

                    <legend id='toirG'><style id='toirG'><dir id='toirG'><q id='toirG'></q></dir></style></legend>

                  1. <small id='toirG'></small><noframes id='toirG'>

                    • <bdo id='toirG'></bdo><ul id='toirG'></ul>

                    <tfoot id='toirG'></tfoot>