How to convert the following table to JSON with javascript?

后端 未结 6 1338
孤城傲影
孤城傲影 2020-11-27 19:15

How to make the following table into a JSON string in jquery/javascript?

6条回答
  •  猫巷女王i
    2020-11-27 19:34

    Try this.

    var myRows = { myRows: [] };
    
    var $th = $('table th');
    $('table tbody tr').each(function(i, tr){
        var obj = {}, $tds = $(tr).find('td');
        $th.each(function(index, th){
            obj[$(th).text()] = $tds.eq(index).text();
        });
        myRows.myRows.push(obj);
    });
    alert(JSON.stringify(myRows));
    

    Working demo - http://jsfiddle.net/u7nKF/1/

提交回复
热议问题
Column 1