如何在jquery / 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>
我想这样做,我可以在一个变量中获得一个JSON字符串&#34; myjson&#34;可以在POST请求或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 :(得分:25)
更新:有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"}]}
答案 1 :(得分:3)
除了能够忽略列,覆盖值以及不被嵌套表混淆之外,我需要相同的东西。我最终写了一个jQuery插件table-to-json:
https://github.com/lightswitch05/table-to-json
您所要做的就是使用jQuery选择表并调用插件:
var table = $('#example-table').tableToJSON();
以下是它的实际演示:
答案 2 :(得分:3)
这是受this article启发的不带jQuery的解决方案:
function tableToJson(table) {
var data = [];
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.push(tableRow.cells[j].innerHTML);;
}
data.push(rowData);
}
return data;
}
答案 3 :(得分: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/
答案 4 :(得分: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。
答案 5 :(得分: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));
答案 6 :(得分:0)
var _table = document.getElementById("table");
var _trLength = _table.getElementsByTagName("tr").length;
var _jsonData = [];
var _obj = {};
var _htmlToJSON = function(index){
var _tr = _table.getElementsByTagName("tr")[index];
var _td = _tr.getElementsByTagName("td");
var _arr = [].map.call( _td, function( td ) {
return td.innerHTML;
}).join( ',' );
var _data = _arr.split(",");
_obj = {
column1 : _data[0]
,column2 : _data[1]
,column3 : _data[2]
};
_jsonData.push(_obj);
};
for(var i = 1; i < _trLength; i++){
_htmlToJSON(i);
}
console.log("html to JSON",_jsonData);