This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes How to return JSON? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to return JSON?" Watch "How to return JSON?" New topic
Author

How to return JSON?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, I am making an ajax call and returning JSON from my controller. In java script I am using eval function to convert the JSON response into a JSON object. In my controller I am writing this
I am able to get the JSON response in my javascript. My question is am I doing i correct in my controller. Converting jsonData to a string and printing it to the response stream. If I am just sending the jsonData as an object I am getting "not accpeted......." message in my javascript.

Can anyone please tell me whether there any better approaches? I heard something about marshaling and unmarshaling json. What is it?

Thank you all in advance. good day.

PS: Does marshaling means converting a POJO or POJO collection (Collection<POJO>) into a JSON object?


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Till now I am doing it like this. In my Controller I write this
Then I return this JSONObject. I am not using any marshaling / un-marshaling. Is this a right approach? or a more naive approach?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

Is this for your Spring application you have been writing? If it is you should have this moved to the Spring forum as Spring handles this sort of thing for you. If you are not using Spring have a look at the Jackson library for this sort of thing. Here is a short tutorial.

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/


[How To Ask Questions][Read before you PM me]
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

if you go through the doc of JSONObject, it tells to convert java JSONObject to external JSON they use toString method so it is valid one.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Bill Gorder wrote:Is this for your Spring application you have been writing? If it is you should have this moved to the Spring forum as Spring handles this sort of thing for you. If you are not using Spring have a look at the Jackson library for this sort of thing. Here is a short tutorial.

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

It is for my Spring application only. I have put the Jakson libraries in my classpath, annotate the method with @ResponseBody and <mvc:annotation-driven />. When spring find the three combination marshaling / un-marshaling is done automatically. I read this from the same site which you pointed to.

But I am saying that I previously was using my own technique to send json response to client. You can see it in my second post. Is it right I am doing there? Is this what marshaling does?

So I had two questions.

1. Can I use jsonObj.toString() and write it to the response stream? According to Mr. Hemanth's answer now I am believing that I followed the right approach till now.
2. Instead of using marshaling / un-marshaling I used my own approach to convert the database result to a JSONObject. It is in the second post in the same thread. I want to know whether this is right approach or not.

Thank you Mr. Bill and Mr. Hemanth.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Bill Gorder wrote:Is this for your Spring application you have been writing? If it is you should have this moved to the Spring forum as Spring handles this sort of thing for you. If you are not using Spring have a look at the Jackson library for this sort of thing. Here is a short tutorial.

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

I read this link Mr. Bill, it seams like ObjectMapper class does the conversion for me instead of I extracting the details from the POJO and putting it into a JSONObject.

Again when I read in wikipedia, the author wrote Marshaling is something like Serialization. used to save object to a file or transmit over a network and rebuild it at the end side.

So I understand that Marshaling is something which converts POJO into a JSON object by extracting all data using getter methods. Then writing the data to a file or a network.

So if I am going to use Jakson library I need not write the code I used to write previously and save lot of development time. Right?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

Correct Marshalling in this case is the process of serializing your java object into a JSON object. While you could do this process manually as you have shown above libraries like this do that work for you in an optimized and tested way as well as giving you lots of other options as well.

For example if you had a huge object you might consider using the streaming API or if you had methods on your object that should not be marshalled you could use the libraries annotations like @JsonIgnore

see this link
http://wiki.fasterxml.com/JacksonInFiveMinutes

and even more here
http://wiki.fasterxml.com/JacksonDocumentation

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you Mr. Bill
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

Your very welcome.

And for future readers that might find this post on a search. The link spoken of explaining how to return JSON with Spring can be found here:

http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to return JSON?
 
Similar Threads
How to access the JSON reponse from a java client
Spring MVC and JSON array
JSON response to populate listbox
JSON+Jquery
get variable from Json and jquery