gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:49:25 24 4
gpt4 key购买 nike

我有一个带有 Joda DateTime 字段的 JSON。它有一些样本值。但每当我将其转换为对象时,它都会自动采用当前的日期时间而不是 JSON 中存在的日期时间。

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 类策略.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 。然后,实现 JsonSerializerJsonDeserializer对于 DateTime,如 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链接)。您可以像这样注册序列化器/反序列化器:

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

关于java - Joda DateTime 从 JSON 反序列化为当前时间戳而不是提供的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com