Joda DateTime 从 JSON 反序列化为当前时间戳而不是提供的数据

Joda DateTime deserialized from JSON as current timestamp instead of provided data

我有一个带有 Joda DateTime 字段的 JSON。它有一些样本值。但是每当我将它转换为 Object 时,它会自动采用当前 DateTime 而不是 JSON.

中存在的 DateTime

PFB 样本 JSON

[{
"pas": "CSP",
"policyNumber": "ZU131874",
"schemeName": "PepsiCo employee scheme20",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
    "startDate": {
        "dayOfYear": 2,
        "year": 2014,
        "dayOfMonth": 2,
        "dayOfWeek": 4,
        "era": 1,
        "weekOfWeekyear": 1,
        "millisOfSecond": 0,
        "secondOfMinute": 0,
        "minuteOfDay": 0,
        "centuryOfEra": 20,
        "yearOfCentury": 14,
        "hourOfDay": 0,
        "monthOfYear": 1,
        "weekyear": 2014,
        "minuteOfHour": 0,
        "yearOfEra": 2014,
        "secondOfDay": 0,
        "millisOfDay": 0,
        "millis": 1388601000000
    },
    "endDate": null
}
}, {
"pas": "CSP",
"policyNumber": "ZU146271",
"schemeName": "PepsiCo employee scheme7",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
    "startDate": {
        "dayOfYear": 156,
        "year": 2015,
        "dayOfMonth": 5,
        "dayOfWeek": 5,
        "era": 1,
        "weekOfWeekyear": 23,
        "millisOfSecond": 0,
        "secondOfMinute": 0,
        "minuteOfDay": 0,
        "centuryOfEra": 20,
        "yearOfCentury": 15,
        "hourOfDay": 0,
        "monthOfYear": 6,
        "weekyear": 2015,
        "minuteOfHour": 0,
        "yearOfEra": 2015,
        "secondOfDay": 0,
        "millisOfDay": 0,
        "millis": 1433442600000
    },
    "endDate": null
}
}]

我正在使用以下代码将 JSON 个对象列表转换为 Java 个对象列表。

policies = new ArrayList<Policy>();

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader("./src/test/resources/" + "sample-zurich-pensions.json"));
Type listType = new TypeToken<List<Policy>>(){}.getType();
List<Policy> policyList = new Gson().fromJson(jsonElement, listType);
policies.addAll(policyList);

jsonElement 中我得到了准确的值,但在 policyList 中 DateTime 设置为当前日期。

PFB 类 Policy.java

private String pas;
private String policyNumber;
private String schemeName;  
private String policyStatus;
private String productCode;
private BigDecimal totalSavings;
private BigDecimal investmentReturn;
private EffectiveDate effectiveDate;

EffectiveDate.java

private DateTime startDate;
private DateTime endDate;

在从 JSON 反序列化期间,Gson 正在创建一个 new DateTime()(等于当前系统 DateTime)。 JSON 中的字段基于 DateTime 中的 getter,但不存在它们的 setter,因此无法将对象调整为 JSON 表示的时间戳。你最好使用像 ISO 8601. Then, implement a JsonSerializer and JsonDeserializer for DateTime as suggested on the Gson site:

这样的标准日期时间表示
class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {

    @Override
    public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
        return new JsonPrimitive(src.toString());
    }

    @Override
    public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
            throws JsonParseException {
        return new DateTime(json.getAsString());
    }
}

或使用 this post 中提供的解决方案之一(也由 @user2762451 链接)。您可以这样注册 serializer/deserializer:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
Gson gson = gsonBuilder.create();