杰克逊JSON日期格式转换

发布于 2025-02-03 02:07:45 字数 744 浏览 1 评论 0原文

请帮助更改日期格式。

**源** json文件

**目标**员工对象

我需要支持将文件json日期转换为对象的支持。从文件

{
 "dateOfBirth": {
    "year": 1980,
    "month": "MAY",
    "monthValue": 5,
    "dayOfMonth": 4,
    "dayOfYear": 124,
    "dayOfWeek": "WEDNESDAY",
    "chronology": {
      "calendarType": "iso8601",
      "id": "ISO"
    },
    "era": "CE",
    "leapYear": false
  }
}

到对象

{"dateOfBirth": "1980-05-04"}

对象

public class Employee {   
    private LocalDate dateOfBirth;
    //setter
    //getter
}

“ com.fasterxml.jackson.datatype:Jackson-Datatype-jsr310”

日期:java.time.localdate

我的目标是从文件中读取JSON数据并将其映射到对象到对象。

Please help to change date format.

**Source** json file

**Target** Employee Object

I need support for converting file json date to Object. from file

{
 "dateOfBirth": {
    "year": 1980,
    "month": "MAY",
    "monthValue": 5,
    "dayOfMonth": 4,
    "dayOfYear": 124,
    "dayOfWeek": "WEDNESDAY",
    "chronology": {
      "calendarType": "iso8601",
      "id": "ISO"
    },
    "era": "CE",
    "leapYear": false
  }
}

to Object

{"dateOfBirth": "1980-05-04"}

Object

public class Employee {   
    private LocalDate dateOfBirth;
    //setter
    //getter
}

Library
"com.fasterxml.jackson.datatype:jackson-datatype-jsr310"

Date : java.time.LocalDate

My goal is to read json data from file and map it to object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2025-02-10 02:07:45

您可以使用自定义求职者。

public class EmployeeBirthDateDeserializer extends StdDeserializer<LocalDate> {

  public EmployeeBirthDateDeserializer() {
    super(LocalDate.class);
  }

  @Override
  public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode root = parser.getCodec().readTree(parser);
    return LocalDate.of(root.get("year").asInt(), root.get("monthValue").asInt(), root.get("dayOfMonth").asInt());
  }
}

然后仅将其应用于dateofbirth employee使用@jsondeserialize

public class Employee {

  @JsonDeserialize(using = EmployeeBirthDateDeserializer.class)
  private LocalDate dateOfBirth;

  //getters and setter
}

test:

public class Temp {

  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Employee employee = mapper.readValue(ClassLoader.getSystemResourceAsStream("strange-date.json"), Employee.class);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    System.out.println(formatter.format(employee.getDateOfBirth()));
  }
}

prints:1980-05-04

You can use custom deserializer.

public class EmployeeBirthDateDeserializer extends StdDeserializer<LocalDate> {

  public EmployeeBirthDateDeserializer() {
    super(LocalDate.class);
  }

  @Override
  public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode root = parser.getCodec().readTree(parser);
    return LocalDate.of(root.get("year").asInt(), root.get("monthValue").asInt(), root.get("dayOfMonth").asInt());
  }
}

Then apply it only to dateOfBirth field in Employee using @JsonDeserialize:

public class Employee {

  @JsonDeserialize(using = EmployeeBirthDateDeserializer.class)
  private LocalDate dateOfBirth;

  //getters and setter
}

Test:

public class Temp {

  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Employee employee = mapper.readValue(ClassLoader.getSystemResourceAsStream("strange-date.json"), Employee.class);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    System.out.println(formatter.format(employee.getDateOfBirth()));
  }
}

Prints: 1980-05-04.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文