开发者

Getting json by DWR (Direct Web Remoting)

开发者 https://www.devze.com 2023-04-02 21:05 出处:网络
I want to know how can I convert a Java object to JSON by using DWR. I have already tried JsonUtil.toJson(myObjec开发者_如何学运维t), but it gives a NullPointerException at org.directwebremoting.jso

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

JsonUtil.toJson(myObjec开发者_如何学运维t), 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.


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


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!


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 ....

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号