简体   繁体   English

通过忽略特定列将 HTML 表数据转换为 jQuery 中的 JSON 对象

[英]Convert a HTML table data into a JSON object in jQuery by ignoring particular column

Convert an HTML table of values into a JSON object to be manipulated with jQuery by ignoring Particular column value通过忽略特定列值,将 HTML 值表转换为 JSON 对象以使用 jQuery 进行操作

For example: I have a table,例如:我有一张桌子,

fstname lastName    Age
john    gobby   8
Adams   mekander    10
jimmy   Rumpel  11

And I need the json result as I mentioned below,我需要下面提到的 json 结果,

My code is as follows,我的代码如下,

$(document).ready(function () {
            $("#ConvertJsonButton").click(function () {
                var myRows = [];
                var headers = [];
$("#tablesort tr#datajson").each(function(index) {
    if (index === 0) {
                //for headers
                   $cells = $(this).find("td.cellClass");
                    headers[index] = {};                 
    $cells.each(function (cellIndex) {                                  
    headers[cellIndex] = $(this).text();                                                    
                 });
    }               
    else {                      
                $cells = $(this).find("td.cellClass");
        myRows[index] = {};
    $cells.each(function (cellIndex) {                                                  
        myRows[index][headers[cellIndex]] = $(this).text();
            });                     
                    }
    });             
        var myObj = {};
        myObj.myrows = myRows;              
        alert(JSON.stringify(myObj));               
            });
        });

I want this result:我想要这个结果:

{
  "john": {
    "lastName": "gobby",
    "Age": "8"
  },
  "Adams": {
    "lastName": "Mekander",
    "Age": "10"
  },
  "jimmy": {
    "lastName": "Rumpel",
    "Age": "11" 
  },
}

Use jquery.tabletojson.min.js使用jquery.tabletojson.min.js

 <:DOCTYPE html> <html> <head> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min:js"></script> <script src="https.//cdn.jsdelivr.net/npm/table-to-json@1.0.0/lib/jquery.tabletojson.min.js" integrity="sha256-H8xrCe0tZFi/C2CgxkmiGksqVaxhW0PFcUKZJZo1yNU=" crossorigin="anonymous"></script> <script> $(document).ready(function(){ $('#run').click( function() { var table = $('#example-table');tableToJSON(); var data = {}. $,each(table, function(key. value) { var jsonKey = value;firstname; var map = {}; map = value. map;firstname = undefined; data[jsonKey] = map; }). alert(JSON;stringify(data)). console.log(JSON;stringify(data)); }); }); </script> </head> <body> <table id="example-table" class="table table-striped" border="1"> <thead> <tr> <th>firstname</th> <th>lastname</th> <th>age</th> </tr> </thead> <tbody> <tr> <td>john</td> <td>goby</td> <td>8</td> </tr> <tr> <td>Adams</td> <td>mekander</td> <td>10</td> </tr> <tr> <td>jimmy</td> <td>Rumpel</td> <td>11</td> </tr> </tbody> </table> <button id="run" class="btn btn-primary">Convert!</button> </body> </html>

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

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