1

我使用 s3 select 来获取选择性数据并将它们显示在我的前端。我将字节数组转换为缓冲区,然后转换为字符串,如下所示

let dataString = Buffer.concat(records).toString('utf8');

我得到的结果是下面的字符串

 {"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"}
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"}
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"}
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"}
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"}
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"}
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"}
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"}
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"}
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"}
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"}
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}
  

现在我想将它们转换为 json 数组,我得到了如下解决方案

let dataArray = dataString.split('\n');
//remove white spaces and commas etc
dataArray = dataArray.filter(d=> d.length >2);
//change string to json
dataArray = dataArray.map(d=> JSON.parse(d));

现在的问题是我已经用新行拆分了它们,如果 json 被压缩或数据本身可以有新行,则无法工作。

处理这种情况的最佳方法是什么。我想要下面的输出

[{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"},
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"},
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"},
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"},
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"},
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"},
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"},
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"},
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"},
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"},
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"},
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}
]
4

2 回答 2

1

是的,将对象连接成这样的字符串并不是一个好主意。但是,如果您没有其他选择,那么类似的方法应该可以解决问题:

const initialString = `{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"}
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"}
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"}
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"}
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"}
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"}
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"}
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"}
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"}
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"}
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"}
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}`;
 
const json = `[${initialString.replace(/}\s*{/g, '},{')}]`;

const array = JSON.parse(json);
于 2021-02-26T20:35:50.553 回答
1

@smit 请看一下这个解决方案。

let dataString=`{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"}
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"}
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"}
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"}
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"}
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"}
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"}
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"}
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"}
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"}
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"}
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}`;
 
 let dataArray = dataString.match(/{(?:[^{}]*|(R))*}/g);
 dataArray = dataArray.map(d=> JSON.parse(d));
 console.log(dataArray);

于 2021-02-26T20:48:30.833 回答