how to Serializing to JSON in jQuery ?

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:

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


asked Jun 3, 2015 in JAVASCRIPT by rajesh
0 votes

2 Answers

0 votes
o convert an object to a string, use JSON.stringify:

var json_text = JSON.stringify(your_object, null, 2);
To convert a JSON string to object, use JSON.parse:

var your_object = JSON.parse(json_text);
answered Jun 3, 2015 by rajesh
0 votes
var myObj = {foo: "bar", "baz": "wockaflockafliz"};


answered Jun 3, 2015 by rajesh