如何使用Javascript叠加类型从JSON数组构建Object数组?

时间:2011-10-02 08:46:09

标签: java json gwt jsni

我的JSON响应是一个Object(例如Folder)的数组。 我想使用Javascript叠加类型生成一个Folder对象数组。

我尝试了以下方法:

public static final native Folder[] buildFoldersArray(String json) /*-{
    return eval('(' + json + ')');
}-*/;

用法:

Folder[] folders = Folder.buildFoldersArray(myJSON);

注意:文件夹会扩展JavaScriptObject。

我得到的错误:

java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject$ cannot be cast to [Lcom.cnario.project.client.Folder;
    at com.cnario.project.client.Folder$.buildFoldersArray(Folder.java)
    at com.cnario.project.client.view.TreeView.FillTree(TreeView.java:51)
    at com.cnario.project.client.ManageContent$1.onResponseReceived(ManageContent.java:89)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:337)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:218)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Unknown Source)

使用JS Overlay类型检索对象数组的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

问题解决了: 评估应该返回JsArrey<E>而不是E[]

public static final native JsArray<Folder> buildFoldersArray(String json) /*-{      
    return eval('(' + json + ')');
}-*/;

使用示例:

JsArray<Folder> folder = Folder.buildFoldersArray(json);
item = new TreeItem(folder.get(0).getName());