tags:

views:

1496

answers:

2

DWR handles lists of primitives quite straight forward. I could not find whether array of objects returned by a DWR method call represent a JSON object. Any clues? Or do I have to return a JSON string representing the array of objects back to the browser?

+4  A: 

This answer is a little late, but here goes :)

Good news: DWR also handles Java arrays and Collections in a really straight-forward way. Just return them and on client side you'll get JavaScript Array objects. (In typical cases like primitives or Strings inside your array or Collection, that is. If the contents is something more exotic, you may need to define converters; more below.)

Here's a quote from DWR documentation (emphasis mine):

By default all of the following are converted for you without further declaration:

  • All primitive types, boolean, int, double, etc.
  • The Class based versions of the these Boolean, Integer, etc.
  • java.lang.String
  • java.util.Date and the 3 SQL derivatives
  • arrays of the above
  • Collections (Lists, Sets, Maps, Iterators, etc) of the above
  • DOM objects (like Element and Document) from DOM, XOM, JDOM and DOM4J

So you definitely won't need JSON strings for these (although that may be a good option for more complicated data structures).

You can actually return many more kinds of objects without doing a lot of manual work because DWR comes with "converters" for lots of typical uses. For example, to make your custom "bean" style Java objects work in client-side JS, all you need to say in dwr.xml is that you want to use the bean converter:

<convert converter="bean" match="com.company.YourBean" />

Even if your method returns a List (or array) of those bean objects...

public static List<YourBean> getData(){ ... }

... the above configuration suffices, which is pretty nice.

Jonik
A: 

my concern is once i get a ist of object how do i navigate in the java script.

let say my query method is

public List query(String courtName) {

return result; }

now this result contains the reservation insatnces.

good enough it is even geting populated, but i do not know how to handle that inside the call back function( ya i have already defined the bean Reservation as a converter type having setter/getter properties).

after the ajax call is returned i have the data , how to parse it??? if someone can give a small code

As http://directwebremoting.org/dwr/server/dwrxml/converters/bean.html says, the Java objects are converted into JavaScript objects (aka maps, aka associative arrays). So if your bean has a "name" property, you could access that in JS using reservation.name or reservation["name"]. If this doesn't help, consider asking a new question about the problem!
Jonik
Did you get the problem solved?
Jonik