我喜欢把这个txt文件转换成json文件,就像我附上的格式。
here my txt:'
WarehouseId WarehouseName SKU NUMBER SKU Name rackSection gatewayID tagAddress lightActive caseCount
50 Lakewood 45234 Mountain Dew (20oz) 4 1 1 True 24
50 Lakewood 65197 Dr Pepper (20oz) 5 1 2 True 24
50 Lakewood 45206 Diet Dr Pepper (20oz) 5 1 3 True 24
50 Lakewood 65209 Diet Pepsi (20oz) 6 1 4 True 24
我试图使用data.split(" ")
,但在那之后,Idk下一步是什么,以及如何使它成为json文件。
这是我想实现的结果(一个json文件):
[
{
"warehouseId" : 50,
"WarehouseName":"Lakewood",
"SKU NUMBER":45234,
"SKU Name":"Mountain Dew (20oz)",
"rackSection":4,
"gatewayID":1,
"tagAddress":1,
"lightActive":"True",
"caseCount":24
},
{
"warehouseId" : 50,
"WarehouseName":"Lakewood",
"SKU NUMBER":65197,
"SKU Name":"Dr Pepper (20oz)",
"rackSection":5,
"gatewayID":1,
"tagAddress":2,
"lightActive":"True",
"caseCount":24
},
{
"warehouseId" : 50,
"WarehouseName":"Lakewood",
"SKU NUMBER":45206,
"SKU Name":"Diet Dr Pepper (20oz)",
"rackSection":5,
"gatewayID":1,
"tagAddress":3,
"lightActive":"True",
"caseCount":24
},
{
"warehouseId" : 50,
"WarehouseName":"Lakewood",
"SKU NUMBER":65209,
"SKU Name":"Diet Pepsi (20oz)",
"rackSection":6,
"gatewayID":1,
"tagAddress":4,
"lightActive":"True",
"caseCount":24
}
]```
Many thanks.
这个问题可能会以重复的形式关闭,但这里有一个简单的解决方案:
const lines = file.split('n');
const headers = lines[0].split('t');
let data = [];
for(let i = 1; i < lines.length; i++) {
const line = lines[i].split('t');
const obj = {};
for(let j = 0; j < headers.length; j++) {
obj[headers[j]] = line[j];
}
data.push(obj);
}