• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript array to JSON object via jquery

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is probably going to be a nice easy answer, but I've been searching all over for a nice simple and clean solution and have yet to find one. Let me elaborate...

I have a javascript array of order numbers that I need to get across to my servlet in a json format because that seems to be the easiest solution for passing data back and forth from the browser to the server...




Can't I do something like...


???

I haven't found anything like this anywhere and I'm just wondering what the easiest way is to do this? Or if I should be doing something different to get my array to the servlet...

Any thoughts?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could do something like that, but I'd not use JSON to go to the server. JSON is great for data coming from the server, because it's a natural format for JavaScript to digest. Not so much for the other direction.

The most natural way for server code to get array data (especially if using Servlets) is the traditional query parameter format.

That said, JSON server libraries are getting more popular (I use Stringtree) so if you did want to pass a JSON string back to the server, I'm sure you can find a way to digest it. I'm just not so sure it's the best (aka simplest) approach.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using stringtree as well. And I'm sure that I can digest it. I can't just plop my array as a parameter on the end of the url...so how do I get this in a parameter format?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could simply iterate over the array with $.each() to format a query string of:


In the servlet:

(If the value will consist of anything other than alphanumerics be sure to URI-encode the values.)

Now, if you are getting your JavaScript array from form DOM elements, there may be easier ways that don't involve the intermediary JavaScript array.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do get the selected orders from the current page of my grid but the array could also have saved values from other pages in the grid that are not a part of the DOM at the point when I want to send the request to the server... so a purely DOM solution won't work. I'll have to probably just format like you suggested...

Thanks for the help. I still wish there was an easy way in jquery to create JSON from an array, or any object for that matter, that can be passed to the server. String tree is pretty good at handling JSON and I think that it would be a nice solution especially for simpler objects like arrays.

just my $.02
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a proposal for a jQuery plugin. Volunteering?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toJSON

hacked the original json.js into a jQuery plugin. It adds the two functions:$.toJSON(value),$.parseJSON(json_str, [safe]).

Never used it. Just found it.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found that one too, but I haven't tried it. Doesn't he say something about not using it with 1.2? One of those places did when I was poking around...I have tossed around the idea of giving that plugin a whirl..... eeee
reply
    Bookmark Topic Watch Topic
  • New Topic