当我包含数据库中的时间戳字段时,我在将json转换为java类型的数组列表时遇到了麻烦。
请注意,如果没有时间戳字段(其余的字段,这里没有显示),转换工作正常。只是当我试图在下面包含时间戳字段时,它失败了,并出现com.google.gson.JsonSyntaxException。
我在这里看到了关于在GsonBuilder上使用setDateFormat的答案,例如GSON - Date format
所以我试了一下,但是没能成功。我不知道在我想要一个类型化数组列表的上下文中该怎么做。我找到的例子都不是这样的。
下面是代码:
Gson gson = new Gson();
TypeToken<List<Resource>> token = new TypeToken<List<Resource>>() {
};
List<Resource> resources = gson.fromJson(response, token.getType());
Map对象具有以下字段:
private Timestamp created;
private Timestamp modified;
以及相应的getter和setter:
/**
* @return the created
*/
public Timestamp getCreated() {
return created;
}
/**
* @param created the created to set
*/
public void setCreated(Timestamp created) {
this.created = created;
}
/**
* @return the modified
*/
public Timestamp getModified() {
return modified;
}
/**
* @param modified the modified to set
*/
public void setModified(Timestamp modified) {
this.modified = modified;
}
编辑:
根据请求,以下是完整的异常消息(我得到的消息):
Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: 2014-03-02 15:33:55
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at model.DataAccess.getResources(DataAccess.java:82)
at se.expertinfo.ccmsliteplugin.PluginHelper.doSearch(PluginHelper.java:132)
at views.SearchView.triggerSearchEvent(SearchView.java:227)
at views.SearchView.valueChanged(SearchView.java:213)
at javax.swing.JTree.fireValueChanged(JTree.java:2919)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3378)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:634)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1092)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:293)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:187)
at javax.swing.JTree.setSelectionPath(JTree.java:1631)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2396)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3612)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressedDND(BasicTreeUI.java:3589)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3548)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:154)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.text.ParseException: Unparseable date: "2014-03-02 15:33:55"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
... 69 more
编辑2:
示例Json(很短,实际结果很长,但语法是一样的):
[
{
"id": "16",
"title": "Warranty 43456",
"created_by": "Anders Svensson",
"modified_by": "Anders Svensson",
"version": "1",
"version_displayname": "1.0",
"informationtype": "document",
"filetype": "xml",
"base_id": "uuid_03372934-a213-11e3-93de-2b47e5a489c2",
"resource_id": "uuid_03372934-a213-11e3-93de-2b47e5a489c2",
"created": "2014-03-02 15:33:55",
"modified": "2014-03-02 15:34:51",
"categories": "10,product,Audi A6;9,product,Audi A4;21,subject,Safety;8,product,Audi A3;21,subject,Safety;15,product,Volvo V70"
},
{
"id": "22",
"title": "Transportation 37337",
"created_by": "Anders Svensson",
"modified_by": "Anders Svensson",
"version": "1",
"version_displayname": "1.0",
"informationtype": "document",
"filetype": "xml",
"base_id": "uuid_03372b96-a213-11e3-93de-2b47e5a489c2",
"resource_id": "uuid_03372b96-a213-11e3-93de-2b47e5a489c2",
"created": "2014-03-02 15:33:55",
"modified": "2014-03-02 15:34:51",
"categories": "10,product,Audi A6;12,product,BMW 320;11,product,BMW 120;13,product,BMW 520;8,product,Audi A3;19,subject,Transportation;9,product,Audi A4;19,subject,Transportation"
}
]
1条答案
按热度按时间9bfwbjaz1#
为您的
Gson
对象设定适当的日期格式。例如,否则,它会尝试使用一些默认值,您可以在
DateTypeAdapter
类源代码中找到这些默认值。