将日期从 json 解析为对象时出错

Error while parsing date from json to object

我有一个 json 作为

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

我正在尝试将此 json 解析为一个对象,该对象的日期对象 (java.util.Date) 为

private Date redemptionStartDate;
private Date redemptionEndDate;

我在解析这些 Date 对象时遇到错误 com.google.gson.JsonSyntaxException。我该如何解决这个问题?

解析方法json

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

在你的class,

private Date redemptionStartDate;
private Date redemptionEndDate;

将此更改为

private long redemptionStartDate;
private long redemptionEndDate;

然后你就可以使用了,

private Date redemptionStartDateVal;
private Date redemptionEndDateVal;

并在这些吸气剂中,将它们的值设置为:

redemptionStartDateVal = new Date(redemptionStartDate);
return redemptionStartDateVal;

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