How to parse arrays of objects returned by DWR?

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?


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="" />

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.

Need Your Help

How to read Java byte code with Eclipse only in the console?

java eclipse console bytecode

The result class file compiled by eclipse and the traditional "javac" command is different. So I want to compile the Java source code in eclipse to get its special output. Also, this output can onl...

<classes> element flipping in and out of existence in storyboard file

ios xcode interface-builder ios5 xcode4.2

This isn't a big problem but it is annoying. Every time I edit my storyboard in Interface Builder and I want to check it into the revision control system there's a element that is either generated...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.