如何使用 Handsontable 发送 Json 数据,使用 Key 作为 Colomn Headers

How to send Json Data using Handsontable using Key's as Colomn Headers

我正在尝试使用列 headers 作为键值对中的键以 json 格式将 handson table 的数据发送到服务器,我得到的输出为

["Rohit","Kunar","DDD","DDD"]

但我希望输出为

{
"SourceIP" : "172.34.32.43,172.23.34.56",
"DestinationIP":"172.34.32.43,123.345.432.345",
"Port": "8080",
"Protocol":"TCP"
}

需要更改什么才能使代码输出这样

我的代码:

jQuery(document).ready(function(){



    var container = document.querySelector('#exampleGrid');

    var hot = new Handsontable(container, {

        startRows: 1,
        startCols: 5,
        minSpareCols: 0,
        //always keep at least 1 spare row at the right
        minSpareRows: 0,
        //always keep at least 1 spare row at the bottom,
        colHeaders: ['Source Ip','Destination Ip','Port','Protocol','Issue'],
        rowHeaders: true,
        //colHeaders: true,
        contextMenu: true,
        persistentState: true,
        manualColumnResize: true,
        manualRowResize: true[![enter image description here][1]][1]
    });

Handsontable.Dom.addEvent(load, 'click', function() {





      console.log(JSON.stringify({hot.getData()}));
  });

你要做的实际上是在你的初始化对象中定义一个data结构。现在你甚至没有定义任何东西(这甚至可能会破坏东西)。所以你应该这样做:

data: []

然后定义您的 columns 对象:

columns: [
    {data:'SourceIP'},
    {data:'DestinationIP'}, 
    {data:'Port'}, 
    {data:'Protocol'}
]

这定义了列的顺序以及对象的创建方式。如果您现在执行 hot.getData(),它将作为对象返回。