0

I want to know how can I convert a Java object to JSON by using DWR. I have already tried

JsonUtil.toJson(myObject), but it gives a NullPointerException at org.directwebremoting.json.JsonUtil.toJson(JsonUtil.java:127). Can anyone tell me any way of converting the Java Object to JSON? I would prefer achieving it by DWR.

asgs
  • 3,928
  • 6
  • 39
  • 54
Akshay
  • 1,735
  • 6
  • 21
  • 29

3 Answers3

1

Why not use the JSON library itself? JSON

Or even the Google-Gson Library GSON

Also, for further reference, use the Search, since similar questions to this one have been answered...

a few examples:

https://stackoverflow.com/questions/338586/a-better-java-json-library

Converting JSON to Java

Community
  • 1
  • 1
Gonçalo Vieira
  • 2,249
  • 2
  • 19
  • 39
0

You should read the documentation as DWR has a tool that creates Json <-> Java mapping automatically. In fact that is the main purpose of DWR!

Angel O'Sphere
  • 2,642
  • 20
  • 18
  • Yes, I know that but for some reason , I need the raw JSON as java string which is produced by DWR. We have been extensively using DWR in the project but this is first time we came across such situation. What are the other libraries available ? – Akshay Sep 07 '11 at 16:09
0

Lets consider this java class.

       class Employee
       {
          int id;
          String eName;
           // setters and getters                
       }   

In javascript JSON object

       var employee = {
                       id   : null,
                       name : null
                      };

This is the call to java method from javascript function.

       EmployeeUtil.getRow(employee,dwrData);

In getRow() of EmployeeUtil class, return type of method will be Employee.

       Employee getRow();

So using the setters of Employee set the data.

dwrData is the callback function.

       function dwrData(data) {
                                employee=data;                   
                              }

The data returned which is Employee bean will be in callback function.

Just initialize this in javascript JSON object.

Hope this helps ....

Srikanth Venkatesh
  • 2,812
  • 1
  • 20
  • 13