Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting java object to json

 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have used the Java APIs from JSON Home earlier. Found them pretty good to work with.

Regards,
Amit
 
James Peter
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

thanks. it is useful.

Rgds
Peter
 
Jesus Mireles
Ranch Hand
Posts: 122
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

thanks for clarifications.

Rgds
Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic