gpt4 book ai didi

javascript - GWT 中带有 JavaScript 覆盖类型列表的 JsonUtils 异常

转载 作者:行者123 更新时间:2023-11-30 09:01:16 26 4
gpt4 key购买 nike

我从 Servlet 获得了一个 JSON 字符串,我想使用 JavaScript 覆盖类型解析这个字符串。

我的整个编码都基于您发布的示例 google我的目标是将 JSON 字符串加载到 Article 类型的 ArrayList 中。

所以我创建了类 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]; }-*/;
}

文章数据类

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; }-*/;
}

在我的 EntryPoint 中,我都按照以下方式上课:

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"}]}

我总是得到这个异常(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)

第 92 行是上面的 getArticles 方法。

我严格遵循谷歌文档,在谷歌搜索异常时没有找到任何有用的结果。仅类文档。

BR & 谢谢,迈贝克

最佳答案

为什么 getArticles() 是一个 JSNI 方法? JsonUtils 是一个普通的 GWT 类。

你的方法应该是:

private JsArray<ArticleData> getArticles(String json) {
return JsonUtils.safeEval(json);
};

关于javascript - GWT 中带有 JavaScript 覆盖类型列表的 JsonUtils 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9127219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com