Rajat Nigam Rajat Nigam - 1 year ago 88
JSON Question

Error while parsing date from json to object

I have a json as


and I am trying to parse this json into an object which has date objects(java.util.Date) as

private Date redemptionStartDate;
private Date redemptionEndDate;

I am getting error as com.google.gson.JsonSyntaxException while parsing into those Date objects. How can I resolve this issue?

Method to parse json

public static <T> T fromJson(String json, Class<T> classOfT) {
Gson gson = new Gson();
T obj = gson.fromJson(json, classOfT);
return obj;

Answer Source

In your class,

private Date redemptionStartDate;
private Date redemptionEndDate;

change this to

private long redemptionStartDate;
private long redemptionEndDate;

then u can use,

private Date redemptionStartDateVal;
private Date redemptionEndDateVal;

and in getters of these, set their values as :

redemptionStartDateVal = new Date(redemptionStartDate);
return redemptionStartDateVal;

See : https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html#Date(long)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download