jQuery 将 HTML 表格数据转换为 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2240005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:05:05  来源:igfitidea点击:

Convert a HTML table data into a JSON object in jQuery

jqueryhtmljsonhtml-table

提问by Jimmy

Anyone know how to convert an HTML table of values into a nice JSON object to be manipulated with jQuery?

任何人都知道如何将 HTML 值表转换为一个很好的 JSON 对象以使用 jQuery 进行操作?

回答by Pointy

An HTML table? Like, all the <td>contents in a 2-d array?

一个 HTML 表格?比如,<td>二维数组中的所有内容?

var tbl = $('table#whatever tr').map(function() {
  return $(this).find('td').map(function() {
    return $(this).html();
  }).get();
}).get();

Then just use $.json (or whatever library you want) to turn that into a JSON string.

然后只需使用 $.json (或您想要的任何库)将其转换为 JSON 字符串。

edit— re-written to use the native (shim here) .map()from the array prototype:

编辑— 重写以使用数组原型中的原生(此处.map()shim):

var tbl = $('table#whatever tr').get().map(function(row) {
  return $(row).find('td').get().map(function(cell) {
    return $(cell).html();
  });
});

The jQuery .map()function has the "feature" of flattening returned arrays into the result array. That is, if the callback function returns a value that is itself an array, then instead of that returned array becoming the value of onecell of the .map()result, its elements are each added to the result.

jQuery.map()函数具有将返回数组展平为结果数组的“特性”。也就是说,如果回调函数返回一个本身就是一个数组的值,那么返回的数组不是成为结果的一个单元格的值,.map()而是每个元素都添加到结果中。

It mightwork to use the original jQuery version and just wrap an extra array around the returned values.

可能工作,使用原来的jQuery版本,只是环绕返回值的额外阵列。

回答by John Rumpel

Do you mean the following situation?

你是说下面的情况吗?

Given:

鉴于:

A1 B1 C1 ...
A2 B2 C2 ...
...

Needed:

需要:

{"1": ["A1", "B1", "C1", ...], 
 "2": ["A2", "B2", "C2", ...], ...}

So use the following function which creates a valid JSON-String function without trailing ",":

因此,请使用以下函数创建一个有效的 JSON-String 函数,但不带尾随“,”:

function html2json() {
   var json = '{';
   var otArr = [];
   var tbl2 = $('#dest_table tr').each(function(i) {        
      x = $(this).children();
      var itArr = [];
      x.each(function() {
         itArr.push('"' + $(this).text() + '"');
      });
      otArr.push('"' + i + '": [' + itArr.join(',') + ']');
   })
   json += otArr.join(",") + '}'

   return json;
}

回答by lightswitch05

I needed the same thing except with the ability to ignore columns, override values, and not be confused by nested tables. I ended up writing a jQuery plugin table-to-json:

除了能够忽略列、覆盖值和不被嵌套表混淆之外,我需要同样的东西。我最终编写了一个 jQuery 插件 table-to-json:

https://github.com/lightswitch05/table-to-json

https://github.com/lightswitch05/table-to-json

All you have to do is select your table using jQuery and call the plugin:

您所要做的就是使用 jQuery 选择您的表并调用插件:

var table = $('#example-table').tableToJSON();

Here is a demo of it in action:

这是它的实际演示:

http://jsfiddle.net/Crw2C/173/

http://jsfiddle.net/Crw2C/173/