Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Table to JSON

Tags:

I need to take table rows and convert to JSON.

Any ideas? I have this code here but it does not work.

function tableToJSON(tableID) {     return $(tableID + "  tr").map(function (row) {         return row.descendants().pluck("innerHTML");     }).toJSON(); } 
like image 960
Nate Avatar asked Jun 07 '11 21:06

Nate


People also ask

How do I save HTML form data to JSON file?

stringify($("#emails_form"). serializeArray()); If you want to store formData in a JSON file, you need to post it to the server (e.g. per AJAX) and save it. But in that case, you can simply post the form und convert it to JSON on the server itself.


1 Answers

function tableToJson(table) {     var data = [];      // first row needs to be headers     var headers = [];     for (var i=0; i<table.rows[0].cells.length; i++) {         headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');     }      // go through cells     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[ headers[j] ] = tableRow.cells[j].innerHTML;          }          data.push(rowData);     }             return data; } 

Taken from John Dyer's Blog

like image 101
slandau Avatar answered Oct 23 '22 19:10

slandau