javascript - 将字符串转换为 json 数组

javascript - convert string to json array

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

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"}
]

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

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);

@sumit 请看看这个解决方案。

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);