Iterate through HTML table using jQuery, converting the data in the table into JSON
html, html-table, jquery, json
Solution
First as fredrik pointed out we need to include https://github.com/douglascrockford/JSON-js.
Second we can use jQuery.fn.map and jQuery.fn.get to create an array of arrays (the `tr`:s) which contains the jQuery.fn.text content of the `td` elements:
var AoA = $('table tr').map(function(){
return [
$('td',this).map(function(){
return $(this).text();
}).get()
];
}).get();
var json = JSON.stringify(AoA);
Problem
I came across a case where I have to convert an HTML table data into JSON. In this process I have to iterate through the table and convert one by one (row) into an array and then convert the whole array into JSON. How do I iterate through the table (each row and column)?