所以,我在浏览器中使用python-node js express显示了一些字符串数据。 数据是这样的:
some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01- 2016,32,42,52,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14, 15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01-2015,8,9, 10,05-01-2015,10,11,12,abcdata_end
我想将此数据转换为json,以便:
"intervals": {
"06-01-2016": {
"ds0": {
"0": [
3, 4, 5
],
"1": [
13,14,15
]
},
"ds1": {
"0": [
32, 42, 52
],
"1": [
33, 43, 53
]
}
},
"06-01-2015": {
filling values in similar way as above
}
}
答案 0 :(得分:0)
我花了一些时间在这个问题上,我不确定我的解决方案是否是最佳的,但它确实有效:
var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end';
var indexes = [];
var ds = {};
str.split(',').forEach((el, i) => {
if (el.includes("start") || el.includes("end"))
indexes.push(i);
});
indexes = indexes.reduce((acc, el, i, arr) => {
if (i % 2 == 0)
acc.push([el, arr[i+1]]);
return acc;
}, []);
indexes.forEach(arr => {
var tmp = str.split(',').slice(arr[0] + 1, arr[1]);
var tmpIndex = "";
tmp.forEach(val => {
if (val.length === 3 && val.includes("ds"))
tmpIndex = val;
else
ds[tmpIndex]
? ds[tmpIndex].push(val)
: ds[tmpIndex] = [val];
});
});
Object.keys(ds).forEach(key => {
var tmpIndex = "";
ds[key] = ds[key].reduce((acc, val) => {
if (val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null)
tmpIndex = val;
else
acc[tmpIndex]
? acc[tmpIndex].push(val)
: acc[tmpIndex] = [val]
return acc;
}, {});
Object.keys(ds[key]).forEach(dateKey => {
ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => {
if (i % 3 == 0)
acc.push([val, arr[i+1], arr[i+2]]);
return acc;
}, [])
});
});
var res = {};
Object.keys(ds).forEach(dsNum => {
Object.keys(ds[dsNum]).forEach(date => {
if (res.hasOwnProperty(date)) {
res[date][dsNum]
? res[date][dsNum].push(ds[dsNum][date])
: res[date][dsNum] = [ds[dsNum][date]];
} else {
res[date] = {[dsNum]: [ds[dsNum][date]]}
}
})
})
Object.keys(res).forEach(date => {
Object.keys(res[date]).forEach(ds => {
res[date][ds] = res[date][ds][0];
})
})
简短的版本,同样的,但有很多丑陋的东西,技巧和嵌套的三元:
var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,32,42,52,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end', indexes = [], ds = {}, res = {};
str.split(',').forEach((el, i) => (el.includes("start") || el.includes("end")) ? indexes.push(i) : null);
indexes = indexes.reduce((acc, el, i, arr) => (i % 2 == 0) ? acc.concat([[el, arr[i+1]]]) : acc, []);
indexes.forEach((arr, tmpIndex = "") => str.split(',').slice(arr[0] + 1, arr[1]).forEach(val => (val.length === 3 && val.includes("ds")) ? tmpIndex = val : ds[tmpIndex] ? ds[tmpIndex].push(val) : ds[tmpIndex] = [val]));
Object.keys(ds).forEach((key, tmpIndex = "") => {
ds[key] = ds[key].reduce((acc, val) => ((val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null) ? tmpIndex = val : acc[tmpIndex] ? acc[tmpIndex].push(val) : acc[tmpIndex] = [val]) == undefined ? acc : acc, {});
Object.keys(ds[key]).forEach(dateKey => ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => (i % 3 == 0) ? acc.concat([[val, arr[i+1], arr[i+2]]]) : acc, []));
});
Object.keys(ds).forEach(dsNum => Object.keys(ds[dsNum]).forEach(date => (res.hasOwnProperty(date)) ? res[date][dsNum] ? res[date][dsNum].push(ds[dsNum][date]) : res[date][dsNum] = [ds[dsNum][date]] : res[date] = {[dsNum]: [ds[dsNum][date]]}));
Object.keys(res).forEach(date => Object.keys(res[date]).forEach(ds => res[date][ds] = res[date][ds][0]));
希望它有所帮助,
最好的问候,
答案 1 :(得分:-2)
您可以使用JSON.parse()将json字符串转换为JSON
例如
var jsonstring = '{'key1':'value1','key2':'value2'}';
var JSondata = JSON.parse(jsonstring);
console.log(JSondata);
现在您可以看到您的日志将采用JSON格式
{
'key1':'value1',
'key2':'value2'
}
希望对你有所帮助