【问题标题】:Debugging JSON in GWT (Cross Server)在 GWT(跨服务器)中调试 JSON
【发布时间】:2010-01-22 03:01:59
【问题描述】:

如何将getJson 中的javascript 代码中的jsonObj 传递给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());


    }

}

【问题讨论】:

    标签: gwt cross-server


    【解决方案1】:

    不完全确定如何解决我遇到的这个问题,但我找到了解决方法。 javascript jsonObj 是多维的,我不知道如何操作 java 函数中的类型。因此,我将 jsonObj.ResultSet.Result 传递给我的函数处理程序,然后我可以在 JSONArray 上使用 get("string")。

    【讨论】:

      【解决方案2】:

      toSource() 应该做什么? (我看到的文档只是说“调用 toSource”。) toString() 呢?

      如果您对alert(jsonObj.ResultSet.totalResultsAvailable) 的调用产生了有效结果,则说明 jsonObj 是 JavaScript 对象,而不是数组。在我看来, JSONArray 的构造函数只需要一个 JS 数组(例如 ["item1", "item2", {"key":"value"}, ...] )

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多