3

我正在使用 JSONStream 读取一个大型 JSON 文件,并且我想在处理整个流时调用一个方法。

var JSONStream = require('JSONStream'), es = require('event-stream');

es.pipeline(
  fs.createReadStream('file.json'),
  JSONStream.parse(['features', true]),
  es.map(function (data) {
    console.log("Added data");
  })
);

我怎样才能做到这一点?

4

2 回答 2

4

我使用'event-stream'处理约200kB的文件,其中包含JSON,并且在使用'event-stream'时从未调用'end'事件时遇到问题,如果我在event-stream之后放置.on('end')管道。但是当我把它放在管道之前 - 一切正常!

stream.on('end',function () {
            console.log("This is the End, my friend");                
     }).on('error',function (err) {
                console.error(err);
     }).pipe(es.split())
       .pipe(es.map(function (line, cb) {
                //Do anything you want here with JSON of line
                return cb();
            }));

,

于 2014-05-29T13:42:38.380 回答
1

排序。将读取流保存在变量中并在该读取流上传递 on('end', function) 。

于 2013-02-05T13:28:19.567 回答