java - 使用 jackson 从字符串反序列化 LocalDateTime

标签 java json jackson

我正在尝试反序列化 StringLocalDateTime与 jackson ,但它不起作用。

我有一个带有 LocalDateTime 字段的数据类:

@Data
public class Registration {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime timestamp;
}

我添加了特殊的 Jackson 数据类型模块:
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

序列化适用于:
new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(registration);

结果字符串:
{"timestamp":"2018-09-03 10:09:35"}

但反序列化不适用于:
new ObjectMapper().registerModule(new JavaTimeModule()).readValue(json.traverse(), Registration.class)

作为错误我得到:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2018-09-03 10:09:35": 
    Failed to deserialize java.time.LocalDateTime: 
        (java.time.format.DateTimeParseException) Text '2018-09-03 10:09:35' could not be parsed: 
            Unable to obtain LocalDateTime from TemporalAccessor: 
                {MinuteOfHour=9, NanoOfSecond=0, SecondOfMinute=35, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=10},
                ISO resolved to 2018-09-03 of type java.time.format.Parsed

我错过了什么?我对序列化有效但反序列化无效感到困惑。

MWE: (小型 gradle Java 项目)

主.java:
import java.io.IOException;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class Main {

    public static void main(String[] args) throws IOException {
        Registration registration = new Registration();
        registration.setTimestamp(LocalDateTime.now());

         ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

         String s = objectMapper.writeValueAsString(registration);
         TreeNode treeNode = objectMapper.readTree(s);

         //Fails here:
         Registration registration1 = objectMapper.readValue(treeNode.traverse(), Registration.class);

        System.out.println(registration1);
    }
}

class Registration {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime timestamp;

    public Registration() {
    }

    public LocalDateTime getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(LocalDateTime localDateTime) {
        this.timestamp = localDateTime;
    }
}

构建.gradle:
plugins {
    id 'java'
}

group 'dateMWE'
version '1.0-SNAPSHOT'

sourceCompatibility = 10

repositories {
    mavenCentral()
}

dependencies {
    compile("com.fasterxml.jackson.core:jackson-annotations:2.9.6")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.6")
}

最佳答案

该问题与 JSON 反序列化无关,而与时间格式字符串有关:

pattern = "yyyy-MM-dd hh:mm:ss"

请注意,时间设置为 hh :这是一个 12 小时格式器,需要“AM”或“PM”值。

如果模式更改为
pattern = "yyyy-MM-dd HH:mm:ss"

问题应该得到解决。

关于java - 使用 jackson 从字符串反序列化 LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52146579/

相关文章:

javascript - 使用 Selenium 捕获网络 XHR 日志(带参数的请求/响应)

java - 无法在 Android 应用程序的对象内返回 JSON 对象

java - 如何指定 Jackson 仅使用字段 - 最好是全局的

java - jackson 根据上下文以不同的方式反序列化一个对象

java - 使用 Jersey RxJava 客户端未找到类 rx.Observable 的序列化器

java - 字符串的 ArrayList 到一个字符串

java - Spring 批处理 : Invalid Content was found starting with element 'batch:job'

java - 如何通过 JMX 或代码监视主要垃圾回收后的内存

json - Go 语言 Redis : Map & Slice

sql - 将在线 JSON 文件集转换为关系数据库(SQL Server、MySQL、SQLITE)