我的应用程序中有一个上传文件,它会接收一个加密的文件。进行解密时,我正在另一个文件中写入json。完成此操作后,我有一个500MB的json文件。
注意:我的json的属性是动态的。
如何解析此json,我读到有关 stream-json / Parser 的信息,但无法实现。
const {parser} = require('stream-json/Parser');
static async _recoverObj(pathAnalise) {
let readStream = fs.createReadStream(pathAnalise, {encoding: 'utf8', highWaterMark: 1024});
function createJson(stream) {
let decipher = crypto.createDecipher('aes-128-ecb', 'AmbientalRuralBR');
return new Promise((resolve) => {
stream.on('data', chunk => {
let newDecrypt = decipher.update(chunk, 'base64', 'utf8');
fs.appendFile('./data.json', newDecrypt, function() {
console.log('Saved!');
});
});
stream.on('end', () => {
fs.appendFile('./data.json', '"}', function() {
resolve();
});
});
});
}
const result = await createJson(readStream);
function getParse() {
return new Promise((resolve) => {
let jsonData = './data.json';
let pipeline = fs.createReadStream(jsonData).pipe(parser());
let objectCounter = 0;
pipeline.on('data', data => {
data.name === 'startObject' && ++objectCounter
});
pipeline.on('end', () => {
console.log(`Found ${objectCounter} objects.`);
resolve();
});
});
}
let dataAnalyze = await getParse();
return dataAnalyze;
}
任何人都知道如何进行此解析吗?
我的解决方案:
我使用了https://www.npmjs.com/package/big-json
const json = require('big-json');
static async _recoverObj(pathAnalise) {
var readStream = fs.createReadStream(pathAnalise, {encoding: 'utf8', highWaterMark: 1024 * 2});
function createJson(stream) {
var decipher = crypto.createDecipher('aes-128-ecb', 'AmbientalRuralBR');
return new Promise((resolve) => {
stream.on('data', chunk => {
var newDecrypt = decipher.update(chunk, 'base64', 'utf8');
fs.appendFile('./data.json', newDecrypt, function() {
console.log('Saved!');
});
});
stream.on('end', () => {
fs.appendFile('./data.json', '"}', function() {
resolve();
});
});
});
}
await streamToString(readStream);
function getParse() {
return new Promise((resolve) => {
var jsonData = './max.json';
var myObj = [];
const readStream = fs.createReadStream(jsonData);
const parseStream = json.createParseStream();
parseStream.on('data', function(pojo) {
myObj = pojo;
});
parseStream.on('end', function() {
resolve(myObj);
});
readStream.pipe(parseStream);
});
}
let dataAnalyze = await getParse();
return dataAnalyze;
}
答案 0 :(得分:-1)
一旦包含包含json的文本字符串,就可以使用JSON.parse()来将其转换为对象,以便以后与之交互:
var json = '{"result":true, "count":42}';
obj = JSON.parse(json);
console.log(obj.count);
// expected output: 42
console.log(obj.result);
// expected output: true