我通过JSON得到了这个日期字符串:"29-OCT-21 12.00.00.000000000 AM UTC".我想将其另存为ZonedDateTime数据类型.

我在模型中设置了如下属性:

  @JsonProperty("createdTs")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yy hh.mm.ss.SSSSSSSSS a z", with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
    private ZonedDateTime createdTs;

我得到一个错误:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.ZonedDateTime` from String "29-OCT-21 12.00.00.000000000 AM UTC": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '29-OCT-21 12.00.00.000000000 AM UTC' could not be parsed 

我不知道哪里出了问题.该模式在测试用例中的格式化程序中工作得很好,如下所示:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a z").toFormatter(
        Locale.getDefault());
    locationPayload.setcreatedTs(ZonedDateTime.parse("29-OCT-21 12.00.00.000000000 AM UTC", formatter));

推荐答案

这个问题与JsonFormat无法处理JSON字符串中的复杂日期-时间格式有关.若要解决此问题,可以为ZonedDateTime类型创建自定义反序列化程序.

下面是一个例子:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeDeserializer extends JsonDeserializer<ZonedDateTime> {

    private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a z")
            .toFormatter();

    @Override
    public ZonedDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String dateString = jsonParser.getText();
        return ZonedDateTime.parse(dateString, FORMATTER);
    }
}

您的财产

public class 测试 {

    @JsonProperty("createdTs")
    @JsonDeserialize(using = ZonedDateTimeDeserializer.class)
    private ZonedDateTime createdTs;
//Getter & Settere
}

测试

    public static void main(String[] args) throws JsonProcessingException {
        String s = """
                {
                "createdTs": "29-OCT-21 12.00.00.000000000 AM UTC"
                }
                """;

        测试 t = JsonMapper.builder().findAndAddModules().build().readValue(s, 测试.class);

        System.out.println(t);
    }

Output 测试(createdTs=2021-10-29T00:00Z[UTC])

试一试,让我知道

Json相关问答推荐

使用Jolt将字符串数组转换为JSON对象数组

过go 24小时内判断员事件的EventBridge事件模式

Flutter -控制器问题-JSON API

JOLT转换以根据条件删除json对象

使用 Power BI 中的 Deneb 视觉效果绘制面积图中的 X 轴日期

从 Inno Setup 中的 JSON 文件的每个对象中读取特定字符串

Oracle Apex - 将 JSON 对象分配给变量以返回

Vue 3如何将参数作为json发送到axios get

如何将动态复杂 json 解析为dart 对象或模型

将哈希表转换为 json 后,Powershell 缺少数组

如何在 Django 的模板语言中获取 json 键和值?

如何在 django rest 框架中定义列表字段?

使用 JSONObject 在 Java 中为以下 struct 创建嵌套 JSON 对象?

在视图中将 .Net 对象转换为 JSON 对象

python追加到json对象中的数组

JSON 和 BSON 哪个更轻量级?

如何向 json IAM 策略添加 comments ?

case 类只有一个字段时如何将json转为 case 类

NSURLRequest 中不支持的 URL

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?