[英]How to convert json timezone object to java TimeZone using Gson?
The server provides next response:服务器提供下一个响应:
"timezone": {
"gmtOffset": 7,
"timeZoneId": "Asia/Novosibirsk",
"dstOffset": 7
}
I use Gson
to parse this json
.我使用
Gson
来解析这个json
。 I tried to add field private TimeZone timezone
to my DTO but Gson
does not understand and throws the error:我试图将字段
private TimeZone timezone
添加到我的 DTO 但Gson
不理解并抛出错误:
java.lang.RuntimeException: Failed to invoke public java.util.TimeZone() with no args
java.lang.RuntimeException:无法在没有参数的情况下调用公共 java.util.TimeZone()
Is there an easy way to solve this?有没有简单的方法来解决这个问题?
It is someone`s custom fields set.这是某人的自定义字段集。 Only custom deserializer may help.
只有自定义解串器可能会有所帮助。
public class TimeZoneDeserializer implements JsonDeserializer<TimeZone> {
@Override
public TimeZone deserialize(
JsonElement jsonElement,
Type type,
JsonDeserializationContext jsonDeserializationContext
) throws JsonParseException {
String timezoneId = jsonElement
.getAsJsonObject()
.get("timeZoneId")
.getAsString();
return TimeZone.getTimeZone(timezoneId);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.