2012-03-29 15 views
9

如何在jquery/javascript中將下表轉換爲JSON字符串?如何使用javascript將下表轉換爲JSON?

<table> 
    <thead> 
    <tr> 
     <th>Column 1</th> 
     <th>Column 2</th> 
     <th>Column 3</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>A1</td> 
     <td>A2</td> 
     <td>A3</td> 
    </tr> 
    <tr> 
     <td>B1</td> 
     <td>B2</td> 
     <td>B3</td> 
    </tr> 
    <tr> 
     <td>C1</td> 
     <td>C2</td> 
     <td>C3</td> 
    </tr> 
    </tbody> 
</table> 

我想讓它,這樣我可以在可以在POST請求中使用的變量「myjson」得到一個JSON字符串或GET請求:

{ 
    "myrows" : [ 
    { 
     "Column 1" : "A1", 
     "Column 2" : "A2", 
     "Column 3" : "A3" 
    }, 
    { 
     "Column 1" : "B1", 
     "Column 2" : "B2", 
     "Column 3" : "B3" 
    }, 
    { 
     "Column 1" : "C1", 
     "Column 2" : "C2", 
     "Column 3" : "C3" 
    } 
    ] 
} 

什麼是最好的方式完成這個? (注:可能有不同數量的行,我只想提取文本,而忽略表格中的其他標籤)

+0

你可以給我們一些HTML,所以它會很容易寫的jQuery搭配吧! – Michael 2012-03-29 14:12:18

+0

jQuery如何連接到您的數據庫? (這不是不可能的,但不太可能) – TRiG 2012-03-29 14:13:03

+0

他確實會說POST和GET請求,這將使得這是一個明顯的ajax連接(php或aspx) – Michael 2012-03-29 14:15:03

回答

20

更新:有一個slightly improved fork of the solution (below) on jsFiddle

你只需要走你的表的DOM讀出來......這是甚至沒有接近優化的,但會給你你想要的結果。 (jsFiddle

// Loop through grabbing everything 
var myRows = []; 
var $headers = $("th"); 
var $rows = $("tbody tr").each(function(index) { 
    $cells = $(this).find("td"); 
    myRows[index] = {}; 
    $cells.each(function(cellIndex) { 
    myRows[index][$($headers[cellIndex]).html()] = $(this).html(); 
    });  
}); 

// Let's put this in the object like you want and convert to JSON (Note: jQuery will also do this for you on the Ajax request) 
var myObj = {}; 
myObj.myrows = myRows; 
alert(JSON.stringify(myObj));​ 

和輸出...

{"myrows":[{"Column 1":"A1","Column 2":"A2","Column 3":"A3"},{"Column 1":"B1","Column 2":"B2","Column 3":"B3"},{"Column 1":"C1","Column 2":"C2","Column 3":"C3"}]} 
+0

哇,這是很好的。我剛讀過它。我很佩服。 :) – Michael 2012-03-29 14:27:27

+0

這裏唯一的「問題」是你得到每一行的標題值。你可以緩存它。 – alextercete 2012-03-29 14:34:17

+0

是的,這就是我提到它沒有優化的原因之一。我會稍微修改一下。 – scottheckel 2012-03-29 14:54:22

0

在這裏你去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)); 
2

試試這個。

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)); 

工作演示 - http://jsfiddle.net/u7nKF/1/

1

我的版本的它:

var $table = $("table"), 
    rows = [], 
    header = []; 

$table.find("thead th").each(function() { 
    header.push($(this).html()); 
}); 

$table.find("tbody tr").each(function() { 
    var row = {}; 

    $(this).find("td").each(function (i) { 
     var key = header[i], 
      value = $(this).html(); 

     row[key] = value; 
    }); 

    rows.push(row); 
}); 

Fiddle

+0

謝謝!它幫助我很多因爲你從$ table分開,在我的情況下,這是必要的,因爲我解析wordpress生成的一個html字符串(不要問我爲什麼¬¬) – Cyberdelphos 2016-04-25 18:12:58

相關問題