jQuery 讓 table 資料表轉為 json 物件

From: http://stackoverflow.com/questions/2240005/convert-a-html-table-data-into-a-json-object-in-jquery

An HTML table? Like, all the <td> contents in a 2-d array?

所有的TD標籤內容全都併在一起

var tbl = $(‘table#whatever tr’).map(function() {
    return $(this).find(‘td’).map(function() {
        return $(this).html();
    }).get();
}).get();

Then just use $.json (or whatever library you want) to turn that into a JSON string.

edit — re-written to use the native (shim here) .map() from the array prototype:

範例將每一列存為一物件

var tbl = $(‘table#whatever tr’).get().map(function(row) {
    return $(row).find(‘td’).get().map(function(cell) {
        return $(cell).html();
    });
});

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s