gpt4 book ai didi

java - Gson 转换为具有两种日期格式的 Json 不起作用

转载 作者:行者123 更新时间:2023-11-30 08:31:39 25 4
gpt4 key购买 nike

我有两种来自服务器 Json 的日期格式。 yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd 是我的日期格式。

所以我尝试使用以下代码将此 Json 转换为 Gson

GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

这些是我的日期格式,

private static final String[] DATE_FORMATS = new String[] {
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd"
};

private class DateDeserializer implements JsonDeserializer<Date> {

@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}

}

更新

这是我的 Json 对象,

{
"idpatient": 56,
"diabetesType": null,
"language": null,
"customId": "CUS790",
"diabetesOther": null,
"firstName": "Niro",
"lastName": "Wije",
"email": "sahirml@gmail.com",
"dob": "1989-10-11",
"parentEmail": null,
"gender": "male",
"diagnosedDate": "2016-11-11",
"height": 0,
"weight": 0,
"heightUnit": null,
"weightUnit": null,
"theme": "Lite",
"userName": "weera",
"password": "asdf9999",
"dateCreated": 1478880594000,
"lastUpdated": 1478880594000
}

然后我尝试通过使用它来将 Json 对象转换为 Gson 对象。我收到以下错误日志。

11-11 16:11:17.735 28072-28072/my.api.example W/System.err: com.google.gson.JsonParseException: Unparseable date: "1478880594000". Supported formats: [yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at my.api.example.activities.LoginActivity$DateDeserializer.deserialize(LoginActivity.java:180)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at my.api.example.activities.LoginActivity$DateDeserializer.deserialize(LoginActivity.java:168)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.google.gson.Gson.fromJson(Gson.java:810)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.google.gson.Gson.fromJson(Gson.java:775)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at retrofit.OkHttpCall.access$100(OkHttpCall.java:29)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:94)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err: at java.lang.Thread.run(Thread.java:841)
11-11 16:11:17.755 28072-28072/my.api.example D/LOGIN_ACTIVITY: ERROR_MESSAGE Unparseable date: "1478880594000". Supported formats: [yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]

这意味着此方法不适用于我的日期格式。我该如何处理这个异常?

有什么想法吗?

最佳答案

根据错误,您共享服务器返回日期毫秒而不是您期望的格式。

com.google.gson.JsonParseException:无法解析的日期:“1478880594000”。支持的格式:[yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]。

首先您需要转换为日期,然后根据您的要求进行格式化。

关于java - Gson 转换为具有两种日期格式的 Json 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546688/

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