Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending back JSON text to convert into a javascript array

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to alert request.responseText, and try to change content type to text/plain.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've showed us everything except the most interesting thing: what's the JSON string that you are returning?
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your strings aren't quoted.
 
Lucas Lech
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry, stupid mistake.

Thanks a lot Bear :-)
 
Lucas Lech
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, try xstream for converting objects to json strings as well ;)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently using jQuery.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tips Lucas :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic