views:

24

answers:

1

Hi

I'm trying to populate a Dictionary object in GWT with data from the server. The Dictionary takes a javascript as input, but I want to send in a String. (acctually its an hashmap.toString). How do I create an javascript object from my string (in java ) that Dictionary will accept?

Thanks

A: 

All you need to do is throw that data onto the response as a JS var.

For example, if you put the following var on the response:

var someVar = {
  a: "a",
  b: "b",
};

Then you can use a Dictionary to access it:

Dictionary d = Dictionary.getDictionary("someVar");
String a = d.get("a");
Yuval A
But how do I create that someVar from Java? My values are in a java.util.HashMap. Tried with the JSArray type, but didn't really get it... It's my serviceImpl that shall contain a method that returns something that the Dictionary can be created by. Like this: [code] customerService.getDictContent("key" ,new AsyncCallback<HashMap<String,Object>>() { public void onSuccess(java.util.HashMap<String,Object> result) { dict = Dictionary.getDictionary(result.toString()); GWT.log(dict.get("hello")); }; public void onFailure(Throwable caught) { }; }); [/code]
Andreas Blomqvist
@xworker You can use http://www.json.org/javadoc/org/json/JSONObject.html to construct a JSON object (i.e., Dictionary) from a `java.util.Map`
Jason Hall
Thanks Jason.How do I import the org.json package into my GWT project? Tried this in the gwt.xml:<inherits name="org.json.JSONObject"/>and added the json.jar to war/WEB-INF/libBut I get: Unable to find 'org/json/JSONObject.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
Andreas Blomqvist