使用JSONStream读取大型JSON文件

时间:2018-03-14 22:41:48

标签: javascript json parsing jsonstream

我一直在尝试使用JSONStream阅读文件,但我没有太多的经验,而且很难找到有关它的信息(教程,文档)。

我在这里找到了这段代码:

var fs = require('fs'),
    JSONStream = require('JSONStream');

var stream = fs.createReadStream('tst.json', {encoding: 'utf8'}),
    parser = JSONStream.parse();

stream.pipe(parser);

console.log(parser);

parser.on('root', function (obj) {
  console.log(obj); // whatever you will do with each JSON object
});

我试图将它与json测试文件一起使用:

{
    "colors": [{
            "color": "black",
            "category": "hue",
            "type": "primary",
            "code": {
                "rgba": [255, 255, 255, 1],
                "hex": "#000"
            }
        },
        {
            "color": "white",
            "category": "value",
            "code": {
                "rgba": [0, 0, 0, 1],
                "hex": "#FFF"
            }
        },
        {
            "color": "red",
            "category": "hue",
            "type": "primary",
            "code": {
                "rgba": [255, 0, 0, 1],
                "hex": "#FF0"
            }
        },
        {
            "color": "blue",
            "category": "hue",
            "type": "primary",
            "code": {
                "rgba": [0, 0, 255, 1],
                "hex": "#00F"
            }
        },
        {
            "color": "yellow",
            "category": "hue",
            "type": "primary",
            "code": {
                "rgba": [255, 255, 0, 1],
                "hex": "#FF0"
            }
        },
        {
            "color": "green",
            "category": "hue",
            "type": "secondary",
            "code": {
                "rgba": [0, 255, 0, 1],
                "hex": "#0F0"
            }
        }
    ]
}

我认为它会返回所有对象,但什么也没发生,它甚至不会出现在" parser.on(' root',function(obj)&#34 ;。 我能做些什么来完成这项工作?

1 个答案:

答案 0 :(得分:2)

root事件已从JSONStream移除。请改用data事件。 https://github.com/dominictarr/JSONStream/commit/97d973ac59d0e58748cec98ea87aae36e057d368

还应将JSON路径指定为JSONStream.parse()的参数。对于您的JSON,它可以是JSONStream.parse('colors.*')

所以把所有东西放在一起,应该是

var fs = require('fs'),
    JSONStream = require('JSONStream');

var stream = fs.createReadStream('tst.json', {encoding: 'utf8'}),
    parser = JSONStream.parse('colors.*');

stream.pipe(parser);

parser.on('data', function (obj) {
  console.log(obj); // whatever you will do with each JSON object
});