- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 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 。然后,实现 JsonSerializer
和 JsonDeserializer
对于 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/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!