views:

181

answers:

2

Hi all,
How do I pass the jsonObj from the javascript code in getJson to the java code handleJsonResponse. If my syntax is correct, what do I do with a JavaScriptObject?
I know that the jsonObj contains valid data because alert(jsonObj.ResultSet.totalResultsAvailable) returns a large number :) --- but some how it's not getting passed correctly back into Java.

EDIT: I solved it... by passing in jsonObj.ResultSet.Result to the java function and working on it using a JSONArray.

Thanks.

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
        [email protected]::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());


    }

}
A: 

What is toSource() supposed to do? (The documentation I see for it just says "calls toSource".) What about toString()?

If your call to alert(jsonObj.ResultSet.totalResultsAvailable) yields a valid result, that tells me jsonObj is a JavaScript Object, not an Array. Looks to me like the constructor for JSONArray only takes a JS Array (e.g., ["item1", "item2", {"key":"value"}, ...] )

BobS
A: 

Not exactly sure how to fix this problem that I had, but I found a workaround. The javascript jsonObj is is multidimensional, and I didn't know how to manipulate the types in the java function. So instead, I passed jsonObj.ResultSet.Result to my function handler, and from there I was able to use get("string") on the JSONArray.

Michael