Gson从Json转换为具有日期的arraylist

时间:2014-03-02 20:08:06

标签: java json arraylist gson

当我从数据库中包含时间戳字段时,我无法将json转换为java类型的arraylist。

请注意,如果没有时间戳字段(其余字段,此处未显示),转换工作正常。就在我尝试将下面的时间戳字段包含在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());

映射对象包含以下字段:

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 个答案:

答案 0 :(得分:3)

Gson对象

设置适当的日期格式
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

否则它会尝试使用您可以在DateTypeAdapter类源代码中找到的一些默认值。