Jackson JSON日期格式转换:将嵌套日期结构映射至Java LocalDate对象
LocalDate in Employee Class Got it, let's tackle this problem step by step. You need to convert that nested date object from your JSON into a java.time.LocalDate field in the Employee class using Jackson's JSR310 module. Here's a straightforward approach:
1. Confirm Dependency Setup
First, make sure you have the jackson-datatype-jsr310 dependency in your project (it handles Java 8+ date/time types):
Maven:
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.15.2</version> <!-- Use the latest compatible version --> </dependency>
Gradle:
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2'
2. Create a Custom JSON Deserializer
Since your JSON's dateOfBirth is a nested object (not a standard ISO date string), we'll write a custom deserializer to extract the necessary fields (year, monthValue, dayOfMonth) and build a LocalDate:
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import java.io.IOException; import java.time.LocalDate; public class CustomLocalDateDeserializer extends JsonDeserializer<LocalDate> { @Override public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException { JsonNode dateNode = parser.getCodec().readTree(parser); int year = dateNode.get("year").asInt(); int month = dateNode.get("monthValue").asInt(); int day = dateNode.get("dayOfMonth").asInt(); return LocalDate.of(year, month, day); } }
3. Update the Employee Class
Annotate the dateOfBirth field to tell Jackson to use our custom deserializer:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import java.time.LocalDate; public class Employee { @JsonDeserialize(using = CustomLocalDateDeserializer.class) private LocalDate dateOfBirth; // Getter and Setter public LocalDate getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(LocalDate dateOfBirth) { this.dateOfBirth = dateOfBirth; } }
4. Read JSON and Map to Employee Object
Configure the ObjectMapper to register the JavaTimeModule (required for Jackson to handle LocalDate), then read your JSON file:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.io.File; import java.io.IOException; public class DateConversionDemo { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); // Register the JSR310 module to support Java 8 date types objectMapper.registerModule(new JavaTimeModule()); // Read JSON file and map to Employee Employee employee = objectMapper.readValue(new File("your-input.json"), Employee.class); // Verify the result System.out.println("Date of Birth: " + employee.getDateOfBirth()); // Outputs: 1980-05-04 } }
How It Works:
- The custom deserializer parses the nested
dateOfBirthJSON object, extracts the numeric year, month, and day values. - It uses
LocalDate.of()to construct the correct date object inyyyy-MM-ddformat. - The
JavaTimeModuleensures Jackson can serialize/deserialize Java 8 date/time types properly.
内容的提问来源于stack exchange,提问作者GoutamS

