I have been trying to desalinize a JSON string containing Date but I am getting the following exception-
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '/Date(1458672480000)/': not a valid representation (error: Unparseable date: "/Date(1458672480000)/" (at offset 0))
at [Source: java.io.StringReader@32e26583; line: 1, column: 199]
Code details is as below-
DataModel-
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataModel {
public Integer Capacity;
public Long Id;
public String Name;
public Date StartDate;
public Date EndDate;
public String Message;
public Integer LocationId;
public Boolean IsValid;
public Integer[] NickNames = new Integer[0];
}
JSON string-
{"d":[{"__type":"my.package.name.className","Id":1,"Name":"xxx","PlaceId":2,"Message":"","IsValid":false,"NickNames":[],"StartDate":"\/Date(1458672480000)\/","EndDate":"\/Date(1458689400000)\/","Size":0,"StringStartDate":"2016-03-22T14:48:00-04:00","StringEndDate":"2016-03-22T19:30:00-04:00"}]}
De serialization code-
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
mapper.setDateFormat(dateFormat);
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
TypeReference<HashMap<String,DataModel[]>> typeRef= new TypeReference<HashMap<String,DataModel[]>>(){};
HashMap<String,DataModel[]> newSessions = mapper.readValue(data, typeRef);
Is there any problem in the JSON string? If not, what is the correct way to deserialize it?
StringStartDatein your json that contains the data exactly how you expect it. That field does not occur in your DataModel though. TheStartDatefield in your json contains invalid content and thus cannot be deserialized into ajava.util.Date.