2011-10-02 43 views
0

我的JSON響應是一個Object(例如文件夾)的數組。 我想使用Javascript覆蓋類型生成一個Folder對象數組。如何使用Javascript覆蓋類型從JSON數組中生成一個Object數組?

我嘗試以下方法:

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覆蓋類型的對象數組的正確方法嗎?

回答

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()); 
+0

請不要用'的eval()'!改用'JsonUtil.safeEval'(或者如果你真的需要''unsafeEval'')。 –

+0

@ThomasBroyer,你能詳細說明'eval'和'JsonUtil.safeEval'之間的區別以及如何實現它嗎?也許你可以發表一個關於這個主題的鏈接? – MichaelS

+0

javadoc說明了一切:[safeToEval](http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/core/client/JsonUtils.html#safeToEval%28java.lang .String%29)和[unsafeEval](http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/core/client/JsonUtils.html#unsafeEval%28java.lang。串%29);否則,搜索網絡爲什麼'eval'完全不安全。 –

相關問題