jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Converting java object to json Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Converting java object to json" Watch "Converting java object to json" New topic
Author

Converting java object to json

James Peter
Ranch Hand

Joined: Sep 15, 2010
Posts: 115
Hi All,

Im using struts2. I would like to know which api is the best to convert a java object to json object.

In action class i need to create a java object and send it to the browser.
in the browser using java script i want to access the java object as a json object and do further things.
Please let me know if any one is having solution.

Rgds
Peter
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
Hi,
I have used the Java APIs from JSON Home earlier. Found them pretty good to work with.

Regards,
Amit
James Peter
Ranch Hand

Joined: Sep 15, 2010
Posts: 115
hi,

thanks. it is useful.

Rgds
Peter
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Why not use the JSON Plugin that comes bundled with struts2? There is no work involved other than adding the jar to your lib and defining a result type with the JSONResult class and in your action you just reference that result type.
James Peter
Ranch Hand

Joined: Sep 15, 2010
Posts: 115
hi Lucas,

I am not sure about below item. Please let me know if I am wrong

1. If we use type='json' in result then all our class variables will be exposed in a json format to browser. Which I do not want.


Rgds
Peter


Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Anything that has a getXXX will be returned in the result. However thats easy to solve as you have several options:

1) Use excludeProperties in the result configuration to tell it what properties to exclude
2) Create a new action that will handle your JSON call instead of using the same action

and I'm sure there are other things you may be able to do as well .. these are just what I came up with without looking anything up


James Peter
Ranch Hand

Joined: Sep 15, 2010
Posts: 115
Hi,

thanks for clarifications.

Rgds
Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting java object to json