Reputation: 5212
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?
Upvotes: 0
Views: 1048
Reputation: 630349
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
.
Upvotes: 2
Reputation: 5212
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
Upvotes: 1
Reputation: 28642
You can use Jquery inbuilt function .serializeArray() for more details check this link
Upvotes: 1