This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes sending back JSON text to convert into a javascript array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending back JSON text to convert into a javascript array" Watch "sending back JSON text to convert into a javascript array" New topic
Author

sending back JSON text to convert into a javascript array

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I hope this is the right place to post this question rather than the HTML and Javascript forum.

I have a servlet which calls a POJO and gets back an ArrayList<String> object. I want to convert the contents of this ArrayList into one String in a JSON format, which I can then send back to the client, which should then use a javascript eval function to convert the responseText into an array.

My servlet compiles fine, but when I test to see what the javascript variable, which should now be an array of strings, holds I get 'undefined'. I've tried a few different formats both in my servlet code and on the client side, but I'm not sure quite what I'm doing wrong. Can anyone show me some sample code, both on the java servlet side and on the client javascript side for me to send a JSON string so that the client will convert it into an array. Hope that makes sense.

Here's some of what I've tried so far.

Partial servlet code:



and on the client side:



("para" is just an empty paragraph that I am using to get some test output)

I've also tried:



(ie changing the eval argument)
and:



I've also tried taking out the square brackets in the servlet string.

Thanks for reading this.

Cheers

Joe Lemmer


OCPJP 85%
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Try to alert request.responseText, and try to change content type to text/plain.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

You've showed us everything except the most interesting thing: what's the JSON string that you are returning?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi guys,

Thanks for your responses.

The string which I am sending back is the one that is being put together in the servlet by iterating through an ArrayList<String>. The servlet is for a language program that I'm making. Each String in the ArrayList is composed of a french and english word or phrase separated by a forward slash (/). So, including the square brackets that I think I have to put round the text before I send it over, so that javascript recogises that this is an array when I use the eval function, an example of the jsonArray string would be:



and this is exactly what I get when, as Kengkaj suggested, I use alert(request.responseText)

I'm hoping that I can have a javascript array which I can iterate through for each question as it comes up.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Your strings aren't quoted.
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
hi

first of all you have no quotes, so this is not a correct array, what you need to return would look like
that:

[ 'str1' , 'str2', ... , 'strn']

secondly, you really really should think about using prototypejs or jquery to do such stuff - will make your
life easier

since prototypejs is my favourite, I'll recommend a link ;)) http://www.prototypejs.org/
Cheers,
Lucas
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Oh, sorry, stupid mistake.

Thanks a lot Bear :-)
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
oh, try xstream for converting objects to json strings as well ;)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Lucas Lech wrote:since prototypejs is my favourite, I'll recommend a link...

Even though I wrote a book on Prototype, I no longer recommend it. I've just run into too many problems with it.

But yes, adopting an Ajax-enabled library to do your Ajax for you is definitely called for.
Lucas Lech
Greenhorn

Joined: Dec 10, 2007
Posts: 23
Bear Bibeault wrote:
Even though I wrote a book on Prototype, I no longer recommend it. I've just run into too many problems with it.


would that mean you're now in the jQuery camp or support some other 'player' ? ;)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

I'm currently using jQuery.
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
thanks for the tips Lucas :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending back JSON text to convert into a javascript array
 
Similar Threads
AJAX problem in Internet Explorer
Converting JSONArray to JSONString
Beginning Object-Oriented JavaScript - future
Ajax in JSP
Falling in love with jQuery?