将jquery数据表数据转换为json

时间:2016-01-21 13:07:45

标签: jquery json datatables

我正在使用jquery数据表。我有一张如下表格,

<table id="employees">
  <thead>
     <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
      </tr>
    </thead>
   <tbody>
      <tr>
           <td>1</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>1234</td>
       </tr>
       <tr>
           <td>1</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>1234</td>
       </tr>
     </tbody>
</table>

我正在将表转换为jquery数据表$('#employees').DataTable()

我想将jquery数据表转换为json格式。 请帮我转换为

[{"Id":"1", "Name":"Karthik","Email":"kk@gmail.com","Phone":"1234"}]

4 个答案:

答案 0 :(得分:5)

尝试使用DataTable rows function

 $('#YourDataTableID').DataTable().rows( { search: 'applied' } ).data().toArray();

答案 1 :(得分:3)

首先需要获取列值:

var heads = [];
$("thead").find("th").each(function () {
  heads.push($(this).text().trim());
});

这会给你:

["Id", "Name", "Email", "Phone"]

使用它我们可以在每一行循环并获取值:

var rows = [];
$("tbody tr").each(function () {
  cur = {};
  $(this).find("td").each(function(i, v) {
    cur[heads[i]] = $(this).text().trim();
  });
  rows.push(cur);
  cur = {};
});

所以最后你会:

var heads = [];
$("thead").find("th").each(function () {
  heads.push($(this).text().trim());
});
var rows = [];
$("tbody tr").each(function () {
  cur = {};
  $(this).find("td").each(function(i, v) {
    cur[heads[i]] = $(this).text().trim();
  });
  rows.push(cur);
  cur = {};
});
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<table id="employees">
  <thead>
    <tr>
      <th>Id</th>
      <th>Name</th>
      <th>Email</th>
      <th>Phone</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Karthik</td>
      <td>Kk@gmail.com</td>
      <td>1234</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Praveen</td>
      <td>pp@gmail.com</td>
      <td>5678</td>
    </tr>
  </tbody>
</table>

预览

输出:http://jsbin.com/kuhuzivadi/edit?html,js,console,output

答案 2 :(得分:3)

试试这个

&#13;
&#13;
$(document).ready(function(){

   // 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)
   alert(JSON.stringify(tableToJSON($("#employees"))));
});


function tableToJSON(tblObj){  
   var data = [];
   var $headers = $(tblObj).find("th");
   var $rows = $(tblObj).find("tbody tr").each(function(index) {
   $cells = $(this).find("td");
   data[index] = {};
   $cells.each(function(cellIndex) {
     data[index][$($headers[cellIndex]).html()] = $(this).html();
   });    
});
  return data;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="employees">
  <thead>
     <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
      </tr>
    </thead>
   <tbody>
      <tr>
           <td>1</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>1234</td>
       </tr>
       <tr>
           <td>2</td>
           <td>Karthik</td>
           <td>Kk@gmail.com</td>
           <td>4567</td>
       </tr>
     </tbody>
</table>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

var employees = convertTableToArrayObject();
alert(JSON.stringify(employees));


function convertTableToArrayObject() {

    var employeeObjects = [];
    var table = $('#employees').DataTable();
    var data = table.rows().data();

    for (var i = 0; i < data.length; i++) {
        employeeObjects.push(data[i]);
    }

    return employeeObjects;
}


  • function convertTableToArrayObject: 循环数据表的每一行,并添加到数组对象
  • JSON.stringify(员工): 将数组对象转换为json