【问题标题】:Convert HTML Table into JSON and set new key to JSON将 HTML 表转换为 JSON 并将新键设置为 JSON
【发布时间】:2019-08-10 17:26:42
【问题描述】:

我想把一个html表的数据转换成json设置它的key。

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

我得到的是这个

JSON

Array(){
0: "Sophos"
1: "Complementary"
2: "Codey Ropen"
}

我需要的是这样的

JSON

[{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
},
{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
},
{
Productname:"Sophos",
ProductTypename:"Software",
AssignedPerson:"Codey"
}]

我的桌子

【问题讨论】:

  • 回答了类似的问题here

标签: jquery json html-table


【解决方案1】:

试试这个方法。

var arr1 = [];
var carr = ['product name', 'product type', 'assigned person'];
$("#table-availed-prod tr").map(function(i, tr){
	var arr = {};
	$(this).find('td').map(function(j, td){
       if(carr.indexOf(j) !== -1){
			arr[carr[j]] = $(this).text();
		}
	});
	arr1.push(arr);
});

console.log(arr1);

【讨论】:

    猜你喜欢
    • 2013-09-03
    • 2014-02-23
    • 2015-09-02
    • 2013-07-02
    • 2021-07-05
    • 2020-05-09
    • 2021-08-15
    • 2019-06-13
    相关资源
    最近更新 更多