0

I have array:

var tab = [];
...
   var dane = [];
   dane['przedmiot'] = przedmiot.text();
   dane['godzina'] = idGodziny;
   dane['dzien'] = dzien;
   tab.push(dane);
...

I want send it via ajax by POST so i want convert it to JSON? how do it?

WooCaSh
  • 5,180
  • 5
  • 36
  • 54

3 Answers3

2

First, use an object (not an array) for dane since your assigning key/values, like this:

var dane = {};
dane['przedmiot'] = przedmiot.text();
dane['godzina'] = idGodziny;
dane['dzien'] = dzien;
tab.push(dane);

Then, to send your object (tab) as JSON, use JSON.stringify(tab), for example:

$.post("myPage.something", JSON.stringify(tab));

For older browsers (IE7 and below) that don't support JSON natively, include json2.js.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
1

You can use Jquery inbuilt function .serializeArray() for more details check this link

link text

santosh singh
  • 27,666
  • 26
  • 83
  • 129
1

I solve my problem using another library: http://code.google.com/p/jquery-json/

var tab = [];
...
   var dane = {};
   dane['przedmiot'] = przedmiot.text();
   dane['godzina'] = idGodziny;
   dane['dzien'] = dzien;
   var enc = $.toJSON(dane);
   tab.push(enc);
...
//before sending
var encoded = $.toJSON(tab);

and i send encoded in post

WooCaSh
  • 5,180
  • 5
  • 36
  • 54