0

I have a json as

{"redemptionStartDate":1436950251941,"redemptionEndDate":1500108651941}

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;
  }
Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
Rajat Nigam
  • 271
  • 1
  • 9
  • 26

1 Answers1

2

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)

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43