Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Serializing to JSON in jQuery

I need to serialize an object to JSON. I'm using jQuery. Is there a "standard" way to do this?

My specific situation: I have an array defined as shown below:

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


also, I need to transform this into a string to pass to $.ajax() like this:

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

2 Answers

aashaykumar
I've been using jquery-json for 6 months and it works great. It's very simple to use:

var myObj = {foo: "bar", "baz": "wockaflockafliz"};
$.toJSON(myObj);

// Result: {"foo":"bar","baz":"wockaflockafliz"}
kshitijrana14
No need for jQuery, use:
JSON.stringify(countries); 

Login / Signup to Answer the Question.