views:

44775

answers:

7

I know how to serialize an object to JSON in ASP.NET Ajax, but I'm trying to do things on the client in a less Microsoft-specific way. I'm using jQuery. Is there a "standard" way to do this?

My specific situation: I have an array defined something like this:

var countries = new Array();
countries[0] = 'ga';
countries[1] = 'cd';
...

and I need to turn this into a string to pass to $.ajax() like this:

$.ajax({
    type: "POST",
    url: "Concessions.aspx/GetConcessions",
    data: "{'countries':['ga','cd']}",
...

Edit (clarification)

I realize there are a number of JSON libraries out there, but I'd like to avoid introducing a new dependency (if I'm going to do that, I might as well use ASP.NET Ajax's built-in JSON serializer).

+7  A: 

I haven't used it but you might want to try the jQuery plugin written by Mark Gibson http://jollytoad.googlepages.com/json.js

It adds the two functions:$.toJSON(value),$.parseJSON(json_str, [safe]).

Tahir Akhtar
In jQuery, most good stuff comes in form of plugins. By avoiding plugins you will be re-writing a lot of already written stuff.
Tahir Akhtar
+10  A: 

No, the standard way to serialize to JSON is to use an existing JSON serialization library. If you don't wish to do this, then you're going to have to write your own serialization methods.

If you want guidance on how to do this, I'd suggest examining the source of some of the available libraries.

EDIT: I'm not going to come out and say that writing your own serliazation methods is bad, but you must consider that if it's important to your application to use well-formed JSON, then you have to weigh the overhead of "one more dependency" against the possibility that your custom methods may one day encounter a failure case that you hadn't anticipated. Whether that risk is acceptable is your call.

Adam Bellaire
Writing your own JSON serialization method is bad. There, I said it. :-)
Ryan Duffield
+2  A: 

I've looked through jQuery code. There's no functionality for JSON serialization.

The only way to get this functionality is to plug it in. jquery-json on Google Code. It's like, 2KB. Just merge it with jQuery into one file, but keep sources for future updates.

Alisey
+1  A: 

You can also try JSON.net if you want an alternative to MS Ajax framework stuff. As the name implies, it is .Net based.

David Robbins
+61  A: 

http://json.org/json2.js

Recently recommended by John Resig:

...PLEASE start migrating your JSON-using applications over to Crockford's json2.js. It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.

In fact, I just landed a change in jQuery yesterday that utilizes the JSON.parse method if it exists, now that it has been completely specified.

I tend to trust what he says on matters Javascript :)

pat
+2  A: 

If you don't want to use external libraries there is .toSource() native javascript method, but it's not perfectly cross-browser.

Kain Haart
A: 

var countries = new Array(); countries[0] = 'ga'; countries[1] = 'cd';

after that you can do like var new_countries = countries.join(',')

after. $.ajax({ type: "POST", url: "Concessions.aspx/GetConcessions", data: new_countries, ...

This thing work as json string format.

vaibhav