[英]Debugging JSON in GWT (Cross Server)
如何将jsonObj从getJson中的javascript代码传递到Java代码handleJsonResponse。 如果我的语法正确,我该如何处理JavaScriptObject?
我知道jsonObj包含有效数据,因为alert(jsonObj.ResultSet.totalResultsAvailable)返回了大量数字:) ---但是有些方法没有正确地传递回Java中。
编辑:我解决了它...通过将jsonObj.ResultSet.Result传递给java函数并使用JSONArray对其进行处理。
谢谢。
public native static void getJson(int requestId, String url, MyClass handler) /*-{
alert(url);
var callback = "callback" + requestId;
var script = document.createElement("script");
script.setAttribute("src", url+callback);
script.setAttribute("type", "text/javascript");
window[callback] = function(jsonObj) { // jsonObj DOES contain data
handler.@com.michael.random.client.MyClass::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
window[callback + "done"] = true;
}
document.body.appendChild(script);
}-*/;
public void handleJsonResponse(JavaScriptObject jso) { // How to utilize JSO
if (jso == null) { // Now the code gets past here
Window.alert("Couldn't retrieve JSON");
return;
}
Window.alert(jso.toSource()); // Alerts 'null'
JSONArray array = new JSONArray(jso);
//Window.alert(""+array.size());
}
}
toSource()应该做什么? (我看到的文档只是说“ calls toSource”。)那么toString()呢?
如果您对alert(jsonObj.ResultSet.totalResultsAvailable)
的调用产生了有效的结果,则表明jsonObj是JavaScript对象,而不是数组。 在我看来,JSONArray的构造函数只需要一个JS数组(例如[[“ item1”,“ item2”,{“ key”:“ value”},...])
不确定如何解决我遇到的问题,但是我找到了解决方法。 javascript jsonObj是多维的,我不知道如何操作java函数中的类型。 因此,我改为将jsonObj.ResultSet.Result传递给函数处理程序,从那里我可以在JSONArray上使用get(“ string”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.