我正在尝试解析AIS消息并将其附加到JSON文件的末尾。我从包含AIS消息的文件中获取输入并将其写入JSON文件。我的代码和输出(在JSON文件中)如下:
fs.watchFile(input,function(){
console.log('New updates received');
sentences = fs.readFileSync(input).toString().split("\n");
sentences.forEach(function(line){
console.log(line);
fs.readFile(output,function(err,data){
if(err){
console.log("Could not read file");
}
else{
var json = JSON.parse(data);
console.log(json.boats);
//Now parse that line
var result = parser.parse(line);
if (result.valid == 'VALID'){
//Now lets make objects out of them
var suppValues = result.supportedValues;
var temp = new boat(result['aisType'],result['channel'],result['repeatInd'],result['mmsi'],result['midCountry'],result['midCountryIso'],
result['mmsi'],result['midCountry'],result['midCountryIso'],result['mmsiType'],result['class'], result['navStatus'],result['navStatusStr'],
result['rotStatus'],result['rot'],result['heading'],result['psogStatus'],result['sog'],result['cog'],result['lat'],result['lng'],
result['posAccuracy'],result['utcTsSec'],result['utcTsStatus']);
console.log("Boat = " + temp);
//Lets create objects out of the data that we have
json.boats.push(temp);
fs.writeFile(output,JSON.stringify(json));
}
else{
console.log("Invalid AIS Message");
}
}
});
});
});
//Boat object
function boat(aisType,channel,repeatInd,mmsi,midCountry,midCountryIso,mmsiType,class_,navStatus,navStatusStr,rotStatus,rot,heading,sogStatus,sog,cog,lat,lng,posAccuracy,utcTsSec,utcTsStatus){
this.aisType = aisType;
this.channel = channel;
this.repeatInd = repeatInd;
this.mmsi = mmsi;
this.midCountry = midCountry;
this.midCountryIso = midCountryIso;
this.mmsiType = mmsiType;
this.class_ = class_;
this.navStatus = navStatus;
this.navStatusStr = navStatusStr;
this.rotStatus = rotStatus;
this.rot = rot;
this.heading = heading;
this.sogStatus = sogStatus;
this.sog = sog;
this.cog = cog;
this.lat = lat;
this.lng = lng;
this.posAccuracy = posAccuracy;
this.utcTsSec = utcTsSec;
this.utcTsStatus = utcTsStatus;
}
JSON文件:
{"boats":[{"aisType":3,"channel":"B","repeatInd":1,"mmsi":305885000,"midCountry":"Antigua and Barbuda","midCountryIso":"AG","mmsiType":305885000,"class_":"Antigua and Barbuda","navStatus":"AG","navStatusStr":"Vessel","rotStatus":"A","rot":1,"heading":"At anchor","sogStatus":"LEFT","sog":-3.615863355006045,"cog":35,"lng":0.2,"posAccuracy":180.8},{"aisType":3,"channel":"B","repeatInd":1,"mmsi":305885000,"midCountry":"Antigua and Barbuda","midCountryIso":"AG","mmsiType":305885000,"class_":"Antigua and Barbuda","navStatus":"AG","navStatusStr":"Vessel","rotStatus":"A","rot":1,"heading":"At anchor","sogStatus":"LEFT","sog":-3.615863355006045,"cog":35,"lng":0.2,"posAccuracy":180.8}]}":0.1,"posAccuracy":316.2}]}"sogStatus":"RIGHT","sog":null,"cog":334,"lng":0.3,"posAccuracy":280.2}]}
正如您在json输出的末尾所看到的,添加了一些不符合JSON语法的内容。在最后有两个花括号是多余的我不明白我是如何得到这样的错误。