How to solve "JSON parse error: Cannot deserialize value of type java.util.Date from String" error

Series Article Directory



foreword

When developing in Java, when processing JSON data and java.util.Date type conversion, sometimes encounter the error message "JSON parse error: Cannot deserialize value of type java.util.Date from String". This error usually occurs when converting a JSON string to a Java object, or when converting a Java object to a JSON string, the date data cannot be parsed due to a date format mismatch. This article will introduce the reasons for this error in detail, and provide solutions to ensure that the serialization and deserialization of date data are handled correctly to avoid this error.


1. Analysis of the cause of the error

"JSON parse error: Cannot deserialize value of type java.util.Date from String" errors typically have several causes:

a. Date format mismatch: The date format in the JSON data does not match the java.util.Date type in Java, resulting in failure to parse.

b. Lack of date formatting annotations: When deserializing JSON data, the corresponding date formatting annotations are missing, resulting in the inability to correctly convert date strings into java.util.Date objects.

c. Using an incorrect date formatting class: When using a date formatting class, you may have chosen the wrong date formatting class or used an outdated class.

Two, the solution

Below we will provide solutions for the above possible reasons:

a. Date format mismatch

Make sure the date format in the JSON data matches the java.util.Date type in Java. Typically, date types in Java are in ISO 8601 format, such as "yyyy-MM-dd'T'HH:mm:ss.SSSZ". When interacting with front-end and back-end data, ensure that the format of the date data is consistent.

If the date format of the JSON data does not match the java.util.Date type in Java, you can use the @JsonFormat annotation in Java to specify the date format:

Guess you like

Origin blog.csdn.net/pleaseprintf/article/details/131949522