我從一個Servlet UND JSON字符串要解析使用JavaScript覆蓋類型此字符串。JsonUtils異常使用JavaScript覆蓋類型一覽表GWT
我的整個編碼都是針對您的示例,由google 發佈我的目標是將JSON字符串加載到Type Article的ArrayList中。
所以我創建的Class JSArray
public class JsArray<E extends JavaScriptObject> extends JavaScriptObject {
protected JsArray() { }
public final native int length() /*-{ return this.length; }-*/;
public final native E get(int i) /*-{ return this[i]; }-*/;
}
類ArticleData
public class ArticleData extends JavaScriptObject {
protected ArticleData() {}
public final native String getId() /*-{ return this.id; }-*/;
public final native String getAmount() /*-{ return this.amount; }-*/;
public final native String getPct() /*-{ return this.pct; }-*/;
public final native String getStartAmount() /*-{ return this.startamount; }-*/;
public final native String getPrice() /*-{ return this.price; }-*/;
public final native String getStockValue() /*-{ return this.stockvalue; }-*/;
}
而在我的入口點我都班方式如下:
String json = event.getResults();
logger.info(json);
JsArray<ArticleData> cs = getArticles(json);
for (int i = 0, n = cs.length(); i < n; ++i) {
Window.alert(cs.get(i).getId() + " " + cs.get(i).getPrice());
}
...
private native JsArray<ArticleData> getArticles(String json)/*-{
return JsonUtils.safeEval(json);
}-*/;
我的JSON文件:
{"articles":[
{"amount":"50","id":"1","pct":"50,00","price":"162,37","startamount":"100","stockvalue":"8118,45"},{"amount":"20","id":"2","pct":"20,00","price":"164,83","startamount":"100","stockvalue":"3296,60"},{"amount":"20","id":"3","pct":"20,00","price":"170,40","startamount":"100","stockvalue":"3408,00"},{"amount":"100","id":"4","pct":"100,00","price":"41,32","startamount":"100","stockvalue":"4132,43"},{"amount":"0","id":"5","pct":"0,00","price":"40,04","startamount":"100","stockvalue":"0,00"}]}
我總是得到這樣的例外:
造成的:com.google.gwt.core.client.JavaScriptException: (的ReferenceError):JsonUtils不是在com.google.gwt.dev.shell定義 .BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace的.java:561) 在com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269) 在com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScript的Host.java:91) at net.mybecks.gwt.client.XMLParser.getArticles(XMLParser.java) at net.mybecks.gwt.client.XMLParser.access $ 2(XMLParser.java:92) at net.mybecks .gwt.client.XMLParser $ 2.onSubmitComplete(XMLParser.java:78) 在com.google.gwt.user.client.ui.FormPanel $ SubmitCompleteEvent.dispatch(FormPanel.java:115) 在com.google.gwt。 user.client.ui.FormPanel $ SubmitCompleteEvent.dispatch(FormPanel.java:1) 在com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
92號線是從上面的getArticles方法。
我嚴格按照谷歌的文檔,我不覺得googleing異常任何有用的結果。只有類文件。
BR &感謝, mybecks
謝謝您的回答。投了票。 – mybecks 2012-02-06 12:36:04