Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Html表转换为JSON_Javascript_Jquery_Json - Fatal编程技术网

Javascript 将Html表转换为JSON

Javascript 将Html表转换为JSON,javascript,jquery,json,Javascript,Jquery,Json,我创建了一个示例应用程序,它将html表转换为JSON。问题是JSON没有重复的值,我想从JSON中删除最后两列 下面给出了我生成的JSON [ { "Person Name":"Smith", "Score":"disqualified", "Price":"150", "Tax":"41" }, { "Person Name":"Jackson", "Score":"94", "Price"

我创建了一个示例应用程序,它将html表转换为JSON。问题是JSON没有重复的值,我想从JSON中删除最后两列

下面给出了我生成的JSON

[
   {
      "Person Name":"Smith",
      "Score":"disqualified",
      "Price":"150",
      "Tax":"41"
   },
   {
      "Person Name":"Jackson",
      "Score":"94",
      "Price":"250",
      "Tax":"81"
   },
   {
      "Person Name":"Doe",
      "Score":"80",
      "Price":"950",
      "Tax":"412"
   },
   {
      "Person Name":"Johnson",
      "Score":"67",
      "Price":"750",
      "Tax":"941"
   }
]
但我期望的JSON是

[
   {
      "Person Name":"Jill",
      "Person Name":"Smith",
      "Score":"disqualified"
   },
   {
      "Person Name":"Eve",
      "Person Name":"Smith",
      "Score":"94"
   },
   {
      "Person Name":"John",
      "Person Name":"Smith",
      "Score":"80"
   },
   {
      "Person Name":"Adam",
      "Person Name":"Smith",
      "Score":"67"
   }
]
谁能告诉我如何从表中生成上面的JSON

我的代码如下所示

html代码

<table id='example-table'>
    <thead>
    <tr>
        <th>Person Name</th>
        <th>Person Name</th>
        <th data-override="Score">Points</th>
        <th>Price</th>
        <th>Tax</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td data-override="disqualified">50</td>
        <td>150</td>
        <td>41</td>
    </tr>
    <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
        <td>250</td>
        <td>81</td>
    </tr>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td>80</td>
        <td>950</td>
        <td>412</td>
    </tr>
    <tr>
        <td>Adam</td>
        <td>Johnson</td>
        <td>67</td>
        <td>750</td>
        <td>941</td>
    </tr>
    </tbody>
</table>
<button id="convert-table" >Convert!</button>
$('#convert-table').click( function() {
  var table = $('#example-table').tableToJSON();
  console.log(table);
  alert(JSON.stringify(table));  
});

要删除最后两个字段,请使用“ignoreColumns”选项

并使标题唯一

<th>Person Name</th>
<th>Person SurName</th>
人名
人名
试试这个:

$('#convert-table').click( function() {
var table = $('#example-table').tableToJSON({
    ignoreColumns:[3,4]}
);
 console.log(table);
 alert(JSON.stringify(table));  
});

Jsfiddle:

类似的东西会有用(不是很好,但是)

说明:

您可以使用ignoreColumns来避免使用第3列和第4列

您可以使用标题来更改“标题”(json文件中的键)。但这也需要第一行(带TH的那一行)

因此,在构建json数组之后,我们必须删除第一行

$('#convert-table').click( function() {
    var $table = $('#example-table');

    var table = $table.tableToJSON(
                      {
                         ignoreColumns:[3, 4], 
                         headings: ['FirstName', 'LastName', 'Score']
                       });
    var newTable = $.map(table, function(e){
        return (e.FirstName == "Person Name") ? null : e;
    });
    console.log(newTable);
    alert(JSON.stringify(newTable));  
});

编辑

如果人名的列数是动态的,您可以这样做(假设您永远不想要最后两行)


请参见

不能有重复的键,但可以使用名称数组。例如:

{
  "PersonNames":["John","Smith"],
  "Score":"80"
},

“从行”是一种格式化html数据的方法

$("#del_player").on("click", function() {
    var row_to_del = $("#table_player tbody tr[active=true]");
    var arr = [];

    $.each(row_to_del, function(key, val){
        arr.push(val.outerText.split('\t'));
    });
    console.log(JSON.stringify(arr));
})

有一个名为
人名的键
两次。这样地??忽略最后两列:
tableToJSON({ignoreColumns:[3,4]})@alexP但我两者都需要…..有什么解决方案吗that@RobertRozas没有两个人名不同…应该需要具有不同值的重复人名谢谢。。。。。这可以删除最后两列……但是如何生成具有不同值的重复键(人名)的JSON呢?我认为这个模块不打算这样做。如果可以找到任何允许的模块,则需要使用另一个模块。tableToJSON是一个jquery模块。不管怎么说,为什么您如此迫切地需要重复的列名?也许可以通过另一种方式取消此限制?它不是名字姓氏,而是人名。问题是人名列是动态的,可以是两个或两个以上that@user3253853然后您必须构建自己的函数,该函数将使用正确的参数调用tableToJSON。@顺便说一句,user3253853,你能展示一些可能存在的“不同案例”吗?
{
  "PersonNames":["John","Smith"],
  "Score":"80"
},
$("#del_player").on("click", function() {
    var row_to_del = $("#table_player tbody tr[active=true]");
    var arr = [];

    $.each(row_to_del, function(key, val){
        arr.push(val.outerText.split('\t'));
    });
    console.log(JSON.stringify(arr));
})