3

I am using DWR in my project for AJAX calls. DWR converts javascript objects to java objects by reading the java class. I want to send and receive JSON like structure through DWR.

Eg:

JS Object:

{
  "name" : "TamilVendhan",
  "age" : "24",
  "hobbies" : [
    "gaming",
    "gaming",
    "gaming"
  ],
  "address" : {
    "doorNo" : "122",
    "city" : "Banglore",
    "state" : "Karnataka",
    "country" : "india"
  }
}

The above js object could be converted to Java as below :

Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "TamilVendhan");
map.put("age", "24");
List<String> list = new ArrayList<String>();
list.add("gaming");
list.add("gaming");
list.add("gaming");
map.put("hobbies", list);
Map<String, Object> addr = new HashMap<String, Object>();
addr.put("doorNo",122);
addr.put("city", "banglore");
addr.put("state", "Karnataka");
addr.put("country", "India");
map.put("address", addr);

Is this possible with DWR. If possible, give me some pointers!

Thanks!


Update:

Converting JS objects into Map<String, Object> is possible in DWR. But its one(1st) level only. If you have any nested objects/array, it will not be converted and ends in conversion error.

See this ticket.

  • @nfechner: Thanks for the edit, man. I will correct this from my next question on-wards. –  Nov 14 '11 at 11:08

1 Answers1

1

The latest version of DWR has json support, you need to enable in your web.xml by passing init-param for the DWR Servlet, more information can be found at, also for DWR there is a nice Book named 'DWR Java AJAX Applications'.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • 2
    I looked into it earlier. Its one way only(Java --> JS). It cannot be done in reverse, atleast for now. Maybe in dwr 4.0 see my question update. –  Nov 14 '11 at 12:36