【发布时间】:2020-06-07 11:58:15
【问题描述】:
我在 Firestore 中存储了一个日期。我从 firestore 获得了一个 HashMap<String, Object>,我想从中重新创建我的对象。
在实现 Date 之前,工作代码是:
HashMap<String, Object> document = new HashMap<String, Object>();
document.put("name", "name");
JSONElement jsonElement = gson.toJsonTree(document);
Event event = gson.fromJson(jsonElement , Event.class);
我现在已经添加了字段
@ServerTimestamp
private Date dateOfEvent;
但是当我尝试序列化它时,我收到以下错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期为 STRING,但在路径 $.dateOfEvent 处为 BEGIN_OBJECT
因为 JsonElement “dateOfEvent” 看起来像这样,因为它是 Firestore 时间戳:
{"dateOfEvent": {"nanoseconds":0,"seconds":1584921600}, "name": "test Event"}
感谢您的宝贵时间和帮助。
【问题讨论】:
-
你尝试使用 JsonArray 了吗?检查这个答案:stackoverflow.com/a/15779695/3973113
标签: android serialization google-cloud-firestore timestamp gson