gpt4 book ai didi

javascript - 通过按某些特殊字符或字符序列拆分来解析 JSON 输出

转载 作者:行者123 更新时间:2023-12-03 08:14:51 25 4
gpt4 key购买 nike

一些日志库将 JSON 字符串写入文件,然后解析它们。我正在编写一个用于集成测试的库,并且想知道分割 JSON 字符串的最佳方法是什么,以便我可以创建一个字符串数组,然后一次解析它们。

这将像这样完成:

var path = require('path');
var filePath = path.resolve(appRootPath + '/' + 'test/output/test1.txt');
var rstream = fs.createReadStream(filePath);

var data = '';

rstream
.on('data', function (chunk) {
data += chunk;
})
.on('end', function () { // done
var array = String(dataLength).split(';'); //spit the big string into an array of strings by some token

doTheThing(array.filter(function (item) { //filter and parse the array element by element
return item && item.length > 0;
}).map(function (item) {
return JSON.parse(String(item));
}));

});

下面,我只是输入分号作为描述字符,但是由于显而易见的原因,这还不够好。

所以我想解析像下面这个大字符串这样的东西(原谅像“狗”和“猫”这样的废话):

{"testId":21,"desc":"cats 1"};{"testId":18,"desc":"dogs 1"};{"testId":1,"desc":"makes stuff 1","error":{}};{"testId":22,"desc":"cats 2"};{"testId":19,"desc":"dogs 2"};{"testId":2,"desc":"makes stuff 2","error":{}};{"testId":20,"tests":[{"testId":21,"type":"it-standard","desc":"cats 1","complete":false,"error":null},{"testId":22,"type":"it-standard","desc":"cats 2","complete":false,"error":null}],"testsParallel":[],"loopTests":[],"children":[],"error":null};{"testId":17,"tests":[{"testId":18,"type":"it-standard","desc":"dogs 1","complete":false,"error":null},{"testId":19,"type":"it-standard","desc":"dogs 2","complete":false,"error":null}],"testsParallel":[],"loopTests":[],"children":[{"testId":20}],"error":null};{"testId":3,"desc":"makes stuff 2","error":null};{"testId":4,"desc":"makes stuff 2","error":{}};{"testId":10,"desc":"makes stuff 1","error":{}};{"testId":11,"desc":"makes stuff 2","error":null};{"testId":12,"desc":"makes stuff 3","error":{}};{"testId":14,"desc":"makes stuff 44","error":{}};{"testId":15,"desc":"makes stuff 888","error":{}};{"testId":16,"desc":"makes stuff 999","error":{}};{"testId":6,"desc":"makes stuff chard","error":{}};{"testId":7,"desc":"makes stuff heeee","error":{}};{"testId":8,"desc":"makes stuff raaa","error":{}};{"testId":0,"tests":[{"testId":1,"type":"it-standard","desc":"makes stuff 1","complete":false,"error":null},{"testId":2,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null},{"testId":3,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null},{"testId":4,"type":"it-standard","desc":"makes stuff 2","complete":false,"error":null}],"testsParallel":[{"testId":9,"tests":[{"testId":10,"type":"it-parallel","desc":"makes stuff 1","complete":false,"error":null},{"testId":11,"type":"it-parallel","desc":"makes stuff 2","complete":false,"error":null},{"testId":12,"type":"it-parallel","desc":"makes stuff 3","complete":false,"error":null}],"type":"ParallelTestSet"},{"testId":13,"tests":[{"testId":14,"type":"it-parallel","desc":"makes stuff 44","complete":false,"error":null},{"testId":15,"type":"it-parallel","desc":"makes stuff 888","complete":false,"error":null},{"testId":16,"type":"it-parallel","desc":"makes stuff 999","complete":false,"error":null}],"type":"ParallelTestSet"}],"loopTests":[{"testId":5,"tests":[{"testId":6,"type":"it-loop","desc":"makes stuff chard","complete":false,"error":null},{"testId":7,"type":"it-loop","desc":"makes stuff heeee","complete":false,"error":null},{"testId":8,"type":"it-loop","desc":"makes stuff raaa","complete":false,"error":null}],"type":"LoopTestSet"}],"children":[{"testId":17}],"error":null};

我需要一种巧妙的方法来用一些永远不会出现在输出中的特殊字符来描述可能的 JS 对象(我可以控制输出中最终的内容,但这可能需要我过滤了最终存储在磁盘上的最终用户的数据。

这是如何完成的?我可以使用一些 super 特殊字符或字符序列吗?还是有一种我没有想到的“更聪明”的方法?

最佳答案

使用 JSON.parse() 可以从字符串创建对象。也许可以尝试这样:

  • 将 JSON 文件作为字符串读取

  • 将字符串拆分为数组:string.split(';');

  • 现在使用 JSON.parse 解析循环中的所有数组条目

或者只是在 JSON 文件中创建一个数组并解析这个完整的东西

关于javascript - 通过按某些特殊字符或字符序列拆分来解析 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com