如何正确使用 JsonUtils.safeEval() 中的 JSArray<T> 对象?
How do I use JSArray<T> objects from JsonUtils.safeEval() properly?
我正在使用 RequestBuilder 读取静态 JSON 文件,并且已确认我在响应的 .getText() 方法中看到了预期的 JSON。我已经精简了我正在做的事情,试图找出确切的问题。这是我能做的最简单的事情。
我使用 JsonUtils.safeEval() 将响应解析为我的覆盖类型,并将结果传递给一个函数,该函数会将结果输出到页面上。显然,此方法还有更多内容,但为了简洁起见,我将其省略。
@Override
public void onResponseReceived(Request request, Response response) {
JsArray<JSONLeadTime> tmp = JsonUtils.<JsArray<JSONLeadTime>>safeEval(response.getText());
if (200 == response.getStatusCode()) {
renderLeadTimes(tmp);
} else {
AlertWindow.displayAlert(ERRORS.GENERIC_CLIENT_ERROR);
}
}
这里是覆盖类型定义,供参考。作为记录,还定义了 "equipment" 覆盖类型。您可以从下面的 JSON 中收集定义,或者如果需要我可以将其包括在内。
public class JSONLeadTime extends JavaScriptObject {
protected JSONLeadTime() {}
public final native String getFacility() /*-{ return this.facility; }-*/;
public final native List<equipment> getEquipment() /*-{ return this.equipment; }-*/;
}
以及正在解析的 JSON 的示例:
[{
"JSONLeadTime": {
"facility": "Temperance",
"equipment": [{
"name": "Coil Line",
"value": "coil-line",
"lead-time": 2,
"sales-router-id": [76]
}]
}
}]
当我尝试遍历 JSArray 时,一切正常,直到我尝试访问覆盖类型的方法。这是吐出结果的方法。当我调用 leadTimes.get(i) 时,我可以看到 "leadTime" 是一个 JavaScriptObject,这正是我当时所期望的。当我打电话时 "leadTime.getFacility()" 是我得到 NPE 的地方。
protected void renderLeadTimes(JsArray<JSONLeadTime> leadTimes) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
for (int i=0; i < leadTimes.length(); i++) {
JSONLeadTime leadTime = leadTimes.get(i);
sb.appendEscaped(leadTime.getFacility()); //This is where I get the NPE
if ( leadTime.getEquipment() != null ) {
for ( int x=0; x < leadTime.getEquipment().size(); x++ ) {
equipment e = leadTime.getEquipment().get(x);
sb.appendEscaped(" - " + e.getName());
}
}
}
view.getDivLeadTimes().setInnerSafeHtml(sb.toSafeHtml());
}
我完全不知所措。我不知道为什么这不起作用。我读过的所有内容都说这应该有效。任何尝试的想法都将不胜感激。
在我的代码中,我首先创建了一个类型,如下所示:
/**
* This class may not be directly instantiated, and can only be returned from a
* native method. For example,
*
* <code>
* native JsArray<JavaScriptObject> getNativeArray() /*-{
* return [
* { x: 0, y: 1},
* { x: 2, y: 3},
* { x: 4, y: 5},
* ];
* }-* /;
* </code>
*/
public class TeamMembersJso extends JsArray<TeamMemberJso> {
protected TeamMembersJso() {
}
}
然后我将原始 JSON 数据计算到 JavaScript 覆盖实例中。
TeamMembersJso jsoData = JsonUtils.safeEval(rawData);
不确定这是否与您在代码中直接使用 JsArray<JSONLeadTime>
有关。我使用的方法绝对值得一试,因为它对我来说效果很好。
我使用的是 GWT 2.8.2,但我认为没有什么大的区别。
您的 JSON 数组包含具有单个 JSONLeadTime
属性 的对象,其值将与您的覆盖类型相匹配。您缺少 JsArray 和覆盖类型之间的中间对象。
我正在使用 RequestBuilder 读取静态 JSON 文件,并且已确认我在响应的 .getText() 方法中看到了预期的 JSON。我已经精简了我正在做的事情,试图找出确切的问题。这是我能做的最简单的事情。
我使用 JsonUtils.safeEval() 将响应解析为我的覆盖类型,并将结果传递给一个函数,该函数会将结果输出到页面上。显然,此方法还有更多内容,但为了简洁起见,我将其省略。
@Override
public void onResponseReceived(Request request, Response response) {
JsArray<JSONLeadTime> tmp = JsonUtils.<JsArray<JSONLeadTime>>safeEval(response.getText());
if (200 == response.getStatusCode()) {
renderLeadTimes(tmp);
} else {
AlertWindow.displayAlert(ERRORS.GENERIC_CLIENT_ERROR);
}
}
这里是覆盖类型定义,供参考。作为记录,还定义了 "equipment" 覆盖类型。您可以从下面的 JSON 中收集定义,或者如果需要我可以将其包括在内。
public class JSONLeadTime extends JavaScriptObject {
protected JSONLeadTime() {}
public final native String getFacility() /*-{ return this.facility; }-*/;
public final native List<equipment> getEquipment() /*-{ return this.equipment; }-*/;
}
以及正在解析的 JSON 的示例:
[{
"JSONLeadTime": {
"facility": "Temperance",
"equipment": [{
"name": "Coil Line",
"value": "coil-line",
"lead-time": 2,
"sales-router-id": [76]
}]
}
}]
当我尝试遍历 JSArray 时,一切正常,直到我尝试访问覆盖类型的方法。这是吐出结果的方法。当我调用 leadTimes.get(i) 时,我可以看到 "leadTime" 是一个 JavaScriptObject,这正是我当时所期望的。当我打电话时 "leadTime.getFacility()" 是我得到 NPE 的地方。
protected void renderLeadTimes(JsArray<JSONLeadTime> leadTimes) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
for (int i=0; i < leadTimes.length(); i++) {
JSONLeadTime leadTime = leadTimes.get(i);
sb.appendEscaped(leadTime.getFacility()); //This is where I get the NPE
if ( leadTime.getEquipment() != null ) {
for ( int x=0; x < leadTime.getEquipment().size(); x++ ) {
equipment e = leadTime.getEquipment().get(x);
sb.appendEscaped(" - " + e.getName());
}
}
}
view.getDivLeadTimes().setInnerSafeHtml(sb.toSafeHtml());
}
我完全不知所措。我不知道为什么这不起作用。我读过的所有内容都说这应该有效。任何尝试的想法都将不胜感激。
在我的代码中,我首先创建了一个类型,如下所示:
/**
* This class may not be directly instantiated, and can only be returned from a
* native method. For example,
*
* <code>
* native JsArray<JavaScriptObject> getNativeArray() /*-{
* return [
* { x: 0, y: 1},
* { x: 2, y: 3},
* { x: 4, y: 5},
* ];
* }-* /;
* </code>
*/
public class TeamMembersJso extends JsArray<TeamMemberJso> {
protected TeamMembersJso() {
}
}
然后我将原始 JSON 数据计算到 JavaScript 覆盖实例中。
TeamMembersJso jsoData = JsonUtils.safeEval(rawData);
不确定这是否与您在代码中直接使用 JsArray<JSONLeadTime>
有关。我使用的方法绝对值得一试,因为它对我来说效果很好。
我使用的是 GWT 2.8.2,但我认为没有什么大的区别。
您的 JSON 数组包含具有单个 JSONLeadTime
属性 的对象,其值将与您的覆盖类型相匹配。您缺少 JsArray 和覆盖类型之间的中间对象。