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

时间:2016-02-16 05:12:20

标签: java json datetime gson jodatime

我有一个带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;

1 个答案:

答案 0 :(得分:2)

在从JSON反序列化期间,Gson正在创建new DateTime()(等于当前系统DateTime)。 JSON中存在的字段基于DateTime中的getter,但是没有存在的setter,因此无法将对象调整为JSON表示的时间戳。使用标准日期时间表示(例如ISO 8601)会更好。然后,按JsonSerializer上的建议为DateTime实施JsonDeserializerGson 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链接)。您可以像这样注册序列化器/解串器:

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