简体   繁体   English

将表格HTML转换为JSON

[英]Convert table HTML to JSON

I have this: 我有这个:

<table>
    <tr>
        <th>Name:</th>
        <td>Carlos</td>
    </tr>        
    <tr>
        <th>Age:</th>
        <td>22</td>
    </tr>
</table>

And I need a JSON format. 而且我需要JSON格式。

{"Name":"Carlos","Age": 22}

I've tried with https://github.com/lightswitch05/table-to-json but it doesn't work for the headings in every row :( 我已经尝试过https://github.com/lightswitch05/table-to-json,但是它不适用于每一行的标题:(

EDIT: http://jsfiddle.net/Crw2C/773/ 编辑: http : //jsfiddle.net/Crw2C/773/

Assuming all you need is to get the first/second cells of each row as key/value pairs, you can use .reduce() to iterate of the rows and just grab the text content of .cells[0] and .cells[1] to use as each key/value pair: 假设您只需要获取每行的第一/第二个单元格作为键/值对,则可以使用.reduce()来对行进行迭代,而只需获取.cells[0].cells[1]用作每个键/值对:

 var t = document.querySelector("table"); var j = [].reduce.call(t.rows, function(res, row) { res[row.cells[0].textContent.slice(0,-1)] = row.cells[1].textContent; return res }, {}); document.querySelector("pre").textContent = JSON.stringify(j, null, 2); 
 <table> <tr> <th>Name:</th> <td>Carlos</td> </tr> <tr> <th>Age:</th> <td>22</td> </tr> </table> <pre></pre> 

The Array.prototype.reduce method takes a collection and uses an accumulator to reduce it down to whatever state you want. Array.prototype.reduce方法获取一个集合,并使用一个累加器将其缩减为所需的任何状态。 Here we just reduce it to an object, so we pass one in after the callback. 在这里,我们只是将其简化为一个对象,因此我们在回调之后传递一个。

For every row, we use the first cell's content as the object key, and the second cell's content as the value. 对于每一行,我们将第一个单元格的内容用作对象键,将第二个单元格的内容用作值。 We then return the object from the callback so that it's given back to us in the next iteration. 然后,我们从回调中返回对象,以便在下一次迭代中将其返回给我们。

Finally, .reduce() returns the last thing we returned (which of course is the object we started with), and that's your result. 最后, .reduce()返回我们返回的最后一件事(这当然是我们开始的对象),这就是您的结果。

You can convert the table in the OP to the required format by first converting it to an Object, then using JSON.stringify to get the required string: 您可以先将OP中的表转换为所需的格式,方法是先将其转换为Object,然后使用JSON.stringify来获取所需的字符串:

<table id="t0">
    <tr>
        <th>Name:</th>
        <td>Carlos</td>
    </tr>        
    <tr>
        <th>Age:</th>
        <td>22</td>
    </tr>
</table>

<script>

function tableToJSON(table) {
  var obj = {};
  var row, rows = table.rows;
  for (var i=0, iLen=rows.length; i<iLen; i++) {
    row = rows[i];
    obj[row.cells[0].textContent] = row.cells[1].textContent
  }
  return JSON.stringify(obj);
}

console.log(tableToJSON(document.getElementById('t0'))); // {"Name:":"Carlos","Age:":"22"}"

</script>

However, that is an ad hoc solution, so will need some work to be adapted to a more general case. 但是,这是一个临时解决方案,因此需要做一些工作才能适应更普遍的情况。 It shows the concept though. 它显示了这个概念。

Note that there is no guarantee that the object properties will be returned in the same order as they appear in the table, you may get {"Age:":"22","Name:":"Carlos"} . 注意,不能保证对象属性将按照表中出现的顺序返回,您可能会得到{"Age:":"22","Name:":"Carlos"}

 var t = document.querySelector("table"); var j = [].reduce.call(t.rows, function(res, row) { res[row.cells[0].textContent.slice(0,-1)] = row.cells[1].textContent; return res }, {}); document.querySelector("pre").textContent = JSON.stringify(j); 
 <table> <tr> <th>Name:</th> <td>Carlos</td> </tr> <tr> <th>Age:</th> <td>22</td> </tr> </table> <pre></pre> 

The Table-to-JSON library that you are using is expecting a different format in your table. 您正在使用的Table-to-JSON库在表中期望使用不同的格式。

It is expecting a table with all of your headers in the first row, followed by the data in subsequent rows. 期望有一个表,其中所有标头都位于第一行,随后是数据。

In other words, it's expecting your table to be structured like this 换句话说,期望您的表的结构如下

<table>
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>        
    <tr>
        <td>Carlos</td>
        <td>22</td>
    </tr>
</table>

Here's a forked version of your JSFiddle in which this is working. 这是在其中工作的JSFiddle的分叉版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM