某些日志记录库将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));
}));
});
下面,我只是将分号作为一个划分角色,但是由于显而易见的原因,它不够好。
所以我想解析下面这个大字符串的东西(原谅像#39; dog' cats' cat')这样的废话:
{"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对象,这些对象具有一些永远不会出现在输出中的特殊字符(我可以控制输出中的内容,但可能需要我过滤最终用户在磁盘上的数据。)
这是怎么做到的?我可以使用一些超级特殊字符或字符序列吗?或者是否有一个聪明的'这样做的方式,我没想到?
答案 0 :(得分:1)
使用JSON.parse()可以从字符串中创建一个对象。 也许这样试试吧:
将JSON文件作为字符串
将字符串拆分为数组:string.split(';');
现在使用JSON.parse解析循环中的所有数组条目
或者只是在JSON文件中创建一个数组并解析这个完整的东西