Update: There's a slightly improved fork of the solution (below) on jsFiddle.
You just need to walk the DOM of your table reading it out... this is not even close to optimized but will give you the result you want. (jsFiddle)
// Loop through grabbing everything
var myRows = [];
var $headers = $("th");
var $rows = $("tbody tr").each(function(index) {
$cells = $(this).find("td");
myRows[index] = {};
$cells.each(function(cellIndex) {
myRows[index][$($headers[cellIndex]).html()] = $(this).html();
});
});
// Let's put this in the object like you want and convert to JSON (Note: jQuery will also do this for you on the Ajax request)
var myObj = {};
myObj.myrows = myRows;
alert(JSON.stringify(myObj));
I needed the same thing except with the ability to ignore columns, override values, and not be confused by nested tables. I ended up writing a jQuery plugin table-to-json:
https://github.com/lightswitch05/table-to-json
All you have to do is select your table using jQuery and call the plugin:
Here is a solution without jQuery, inspired by this article:
function tableToJson(table) {
var data = [];
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = [];
for (var j=0; j<tableRow.cells.length; j++) {
rowData.push(tableRow.cells[j].innerHTML);;
}
data.push(rowData);
}
return data;
}