简体   繁体   English

JSON.parse SyntaxError:使用JSON解析文件时出现意外令牌{

[英]JSON.parse SyntaxError: Unexpected token { while parsing file with JSON

I'm having problems trying to parse a file that has the following text in it: 我在尝试解析包含以下文本的文件时遇到问题:

{ "type": "header", "log_level": 3, "target_port": 80, "source_port_first": 32768, "source_port_last": 61000, "max_targets": -1, "max_runtime": 0, "max_results": 0, "iface": "en0", "rate": 0, "bandwidth": 0, "cooldown_secs": 8, "senders": 7, "use_seed": 0, "seed": 0, "generator": 0, "packet_streams": 1, "probe_module": "tcp_synscan", "output_module": "json", "gw_mac": "00:00:00:00:00:00", "source_ip_first": "127.0.0.1", "source_ip_last": "127.0.0.1", "output_filename": ".\/static\/results\/80.json", "whitelist_filename": ".\/static\/whitelist.conf", "dryrun": 0, "summary": 0, "quiet": 1, "recv_ready": 0 }
{ "type": "result", "saddr": "127.0.0.1" }

This is a Zmap output and node.js keeps choking on everything after the first line. 这是一个Zmap输出,node.js在第一行之后一直阻塞所有内容。 If you remove the second line from the file, there are no errors and the program runs fine. 如果从文件中删除第二行,则没有错误,程序运行正常。

I want to be able to read the JSON data in the file and be able to reference each key and value and print them out in console.log. 我希望能够读取文件中的JSON数据,并能够引用每个键和值并在console.log中将它们打印出来。

Here's my current code: 这是我当前的代码:

var fs = require('fs');
var filename = './80.json';
var bufferString;

function ReadFile(callback) {
  fs.readFile(filename, 'utf-8', function(err, data) {
    bufferString = data;
    callback();
  }); 
}

function PrintLine() {
  console.log(JSON.parse(bufferString));
}

ReadFile(PrintLine)

Realistically, I would like to put all this data into a database, but I need to solve the problem of reading the file properly. 实际上,我想将所有这些数据放入数据库中,但是我需要解决正确读取文件的问题。

You can't have multiple JSON objects in a file like that. 这样的文件中不能有多个JSON对象。 If you want to store 2 objects in JSON you need to add them to an array: 如果要在JSON中存储2个对象,则需要将它们添加到数组中:

[
    { "type": "header", ..., "recv_ready": 0 },
    { "type": "result", "saddr": "127.0.0.1" }
]

You can access each object with their index: 您可以访问每个对象及其索引:

var json = JSON.parse(bufferString);
json[0]; // this is the first object (defined on the first line)
json[1]; // this is the second object (defined on the second line)

Like mentioned, the JSON is invalid. 如前所述,JSON无效。 But, instead of turning the JSON in the file into an array of objects, you can also process each line, if each object is on a new line: 但是,如果每个对象都在新行上,那么也可以处理每一行,而不是将文件中的JSON转换为对象数组:

However, be aware that, like @jsve pointed out, your file would then remain a JSON impostor. 但是,请注意,就像@jsve指出的那样,您的文件将仍然是JSON冒名顶替者。

function PrintLine() {
    var lines = bufferString.split('\n'),
        tmp = [],
        len = lines.length;
    for(var i = 0; i < len; i++) {
        // Check if the line isn't empty
        if(lines[i]) tmp.push( JSON.parse(lines[i]) );
    }
    lines = tmp;
    console.log(lines[0], lines[1]);
}

ReadFile(PrintLine);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM