[英]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.