I get a JSON String from a Servlet und want to parse this string using JavaScript Overlay Types.
My whole coding oriented on thy examples published by google
My Goal is to load the JSON String into an ArrayList of Type Article.
So I created the 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]; }-*/;
}
The class 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; }-*/;
}
And in my EntryPoint I both classes to following way:
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);
}-*/;
My JSON file:
{"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"}]}
I always get this exception:
Caused by: com.google.gwt.core.client.JavaScriptException:
(ReferenceError): JsonUtils is not defined
at 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)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.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)
at com.google.gwt.user.client.ui.FormPanel$SubmitCompleteEvent.dispatch(FormPanel.java:115)
at com.google.gwt.user.client.ui.FormPanel$SubmitCompleteEvent.dispatch(FormPanel.java:1)
at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
Line 92 is the getArticles method from above.
I strictly following the google documentation, and I don’t find any helpful results in googleing the exception. Only Class Documetations.
BR & Thanks,
mybecks
Why is
getArticles()a JSNI method?JsonUtilsis a normal GWT class.Your method should be: