I read some var
from my HTML-Page using GWT Dictionary. The var
looks like this:
var test = {
"a" : "123",
"b" : "jg34l",
...
}
Now I get via AJAX-Call new content for my JS var. At the moment I overwrite it like this:
public native void set(String key, String value) /*-{
$wnd["test"][key] = value;
}-*/;
public void onResponseReceived(Request request, Response response) {
JSONObject obj = (JSONObject) JSONParser.parseLenient(response.getText());
for (String key : obj.keySet()) {
JSONString val = (JSONString) obj.get(key);
set(key, val.stringValue());
}
}
As you can see I get a JSON-String. Parse it. Cast it to JSONObject. Take every key-value-pair and use the JSNI-method to set the pairs.
There must be an easier way to do this?! I want simply to say: $wnd["test"] = myJsonObject
Please help me, 'cause it is performance-critical step (up to 1000 key-value-pairs).
You can try using JSO (should be also much faster than JSONObject
)
Something like this:
public native void set(JavaScriptObject data) /*-{
$wnd['test'] = data;
}-*/;
public void onResponseReceived(Request request, Response response) {
JavascriptObject data = JsonUtils.safeEval(response.getText()).case();
set(data);
}
I haven't tried myself, so I can't say 100% if it would work.
Update:
Instead of a Dictionary you can directly return a JavaScriptObject
that supports hash like access:
public class MyDictionary extends JavaScriptObject {
public native String get(String name) /*-{
return this[name];
}-*/;
}
public native MyDictionary getData() /*-{
return $wnd['test'];
}-*/