How to convert the following table to JSON with javascript?

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

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

6条回答
  •  半阙折子戏
    2020-11-27 19:52

    Here you go http://jsfiddle.net/Ka89Q/4/

    var head = [],
        i = 0,
        tableObj = {myrows: []};
    $.each($("#my_table thead th"), function() {
        head[i++] = $(this).text();
    });
    
    $.each($("#my_table tbody tr"), function() {
        var $row = $(this),
            rowObj = {};
    
        i = 0;
        $.each($("td", $row), function() {
            var $col = $(this);
            rowObj[head[i]] = $col.text();
            i++;
        })
    
        tableObj.myrows.push(rowObj);
    });
    
    alert(JSON.stringify(tableObj));
    

提交回复
热议问题
Column 1