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

召集

我正在使用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"}
]
吉田淳

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将JavaScript字符串转换为数组

来自分类Dev

将字符串转换为Javascript中的JSON对象

来自分类Dev

将数组转换为字符串Javascript

来自分类Dev

Javascript ::将JSON字符串发布回调转换为纯JavaScript数组

来自分类Dev

将javascript字符串转换为php数组

来自分类Dev

将字符串数组转换为JAVASCRIPT对象

来自分类Dev

将字符串数组转换为对象Javascript

来自分类Dev

将字符串格式的数组转换为javascript数组

来自分类Dev

Javascript将字符串转换为对象(列表/数组)

来自分类Dev

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

来自分类Dev

如何将动态JSON字符串转换为JavaScript数组

来自分类Dev

将字符串转换为Javascript中的数组?

来自分类Dev

将字符串转换为数组JavaScript

来自分类Dev

将JSON字符串转换为对象Javascript

来自分类Dev

将数组转换为字符串Javascript

来自分类Dev

将字符串转换为JSON obj或数组JavaScript

来自分类Dev

Javascript,将数组转换为这样的字符串

来自分类Dev

将字符串转换为JavaScript中的对象数组

来自分类Dev

javascript将JSON字符串转换为JSON对象

来自分类Dev

将具有重复键的json字符串转换为javascript数组

来自分类Dev

将键值对的JSON数组转换为Javascript中的JSON字符串数组

来自分类Dev

使用 javascript 或 c# 将 JSON 字符串转换为数据数组

来自分类Dev

Javascript:将字符串转换为对象数组?

来自分类Dev

如何让javascript将多维数组转换为字符串?

来自分类Dev

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

来自分类Dev

将字符串转换为数组 JavaScript 中的数字

来自分类Dev

将数组作为字符串转换为数组javascript

来自分类Dev

Javascript 将 JSON 字符串转换为数组数组

来自分类Dev

将字符串转换为多维数组索引 Javascript

Related 相关文章

  1. 1

    将JavaScript字符串转换为数组

  2. 2

    将字符串转换为Javascript中的JSON对象

  3. 3

    将数组转换为字符串Javascript

  4. 4

    Javascript ::将JSON字符串发布回调转换为纯JavaScript数组

  5. 5

    将javascript字符串转换为php数组

  6. 6

    将字符串数组转换为JAVASCRIPT对象

  7. 7

    将字符串数组转换为对象Javascript

  8. 8

    将字符串格式的数组转换为javascript数组

  9. 9

    Javascript将字符串转换为对象(列表/数组)

  10. 10

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

  11. 11

    如何将动态JSON字符串转换为JavaScript数组

  12. 12

    将字符串转换为Javascript中的数组?

  13. 13

    将字符串转换为数组JavaScript

  14. 14

    将JSON字符串转换为对象Javascript

  15. 15

    将数组转换为字符串Javascript

  16. 16

    将字符串转换为JSON obj或数组JavaScript

  17. 17

    Javascript,将数组转换为这样的字符串

  18. 18

    将字符串转换为JavaScript中的对象数组

  19. 19

    javascript将JSON字符串转换为JSON对象

  20. 20

    将具有重复键的json字符串转换为javascript数组

  21. 21

    将键值对的JSON数组转换为Javascript中的JSON字符串数组

  22. 22

    使用 javascript 或 c# 将 JSON 字符串转换为数据数组

  23. 23

    Javascript:将字符串转换为对象数组?

  24. 24

    如何让javascript将多维数组转换为字符串?

  25. 25

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

  26. 26

    将字符串转换为数组 JavaScript 中的数字

  27. 27

    将数组作为字符串转换为数组javascript

  28. 28

    Javascript 将 JSON 字符串转换为数组数组

  29. 29

    将字符串转换为多维数组索引 Javascript

热门标签

归档