File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Javascript array to JSON object via jquery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript array to JSON object via jquery" Watch "Javascript array to JSON object via jquery" New topic
Author

Javascript array to JSON object via jquery

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

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

Joined: Nov 19, 2007
Posts: 269
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Sounds like a proposal for a jQuery plugin. Volunteering?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javascript array to JSON object via jquery