HTML Table to JSON

前端 未结 7 1883
小鲜肉
小鲜肉 2020-11-29 06:56

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 $(t         


        
相关标签:
7条回答
  • 2020-11-29 07:11

    I was unhappy with all the solutions above and ended up writing my own jQuery plugin to accomplish this. It is very similar to the solution but accepts several options to customize the results, such as ignoring hidden rows, overriding column names, and overriding cell values

    The plugin can be found here along with some examples if this is more what your looking for: https://github.com/lightswitch05/table-to-json

    0 讨论(0)
  • 2020-11-29 07:18
    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

    0 讨论(0)
  • 2020-11-29 07:19

    try $("#"+tableID + " tr") instead.

    0 讨论(0)
  • 2020-11-29 07:20

    HTML table with thead and tbody:

        function htmlTableToJson(table, edit = 0, del = 0) {
            // If exists the cols: "edit" and "del" to remove from JSON just pass values = 1 to edit and del params
            var minus = edit + del;
            var data = [];
            var colsLength = $(table.find('thead tr')[0]).find('th').length - minus;
            var rowsLength = $(table.find('tbody tr')).length;
    
            // first row needs to be headers
            var headers = [];
            for (var i=0; i<colsLength; i++) {
                headers[i] = $(table.find('thead tr')[0]).find('th').eq(i).text();
            }
    
            // go through cells
            for (var i=0; i<rowsLength; i++) {
                var tableRow = $(table.find('tbody tr')[i]);
                var rowData = {};
                for (var j=0; j<colsLength; j++) {
                    rowData[ headers[j] ] = tableRow.find('td').eq(j).text();
                }
                data.push(rowData);
            }       
            return data;
        }
    
    0 讨论(0)
  • 2020-11-29 07:26

    This one worked for me: (I had only 2 lines in my table, th and td)

    function htmlToJson(table) {
        var obj = {},
            itemsLength = $(table.find('tbody tr')[0]).find('th').length;
        for (i=0;i<itemsLength;i++) {
            key = $(table.find('tbody tr')[0]).find('th').eq(i).text();
            value = $(table.find('tbody tr')[1]).find('td').eq(i).text();
            obj[key] = value;
        }
        return JSON.stringify(obj)
    }
    
    0 讨论(0)
  • 2020-11-29 07:31

    You should find this helpful: http://encosia.com/use-jquery-to-extract-data-from-html-lists-and-tables/

    0 讨论(0)
提交回复
热议问题