如何解决字符串化为 json 时添加了额外的双引号
嗨即时试图让机器学习算法,结果写入到以.json数据集。当我执行 fs.writefilesync 并将其字符串化时,它会在条目的末尾添加一些随机双引号。这会导致培训计划出现错误,我需要将其删除。这是我的代码:
const brain = require('brain.js')
const prompt = require('prompt-sync')()
const fs = require('fs')
const learn = require('./test.json')
const networkDevice = prompt('what network do you want to use: ')
const netd = (networkDevice + '.json')
let network = new brain.recurrent.LSTM();
function run() {
const networkState = JSON.parse(fs.readFileSync(netd,"utf-8"))
network.fromJSON(networkState)
const input1 = prompt('Text: ')
const input2 = prompt('the facial expression ur making (angry,sad,happy): ')
const input3 = prompt('the tone ur saying it in (happy,angry): ')
let output = network.run(input1,input2)
console.log('Retardbot: ' + output);
const desiredoutput = prompt('desired text(put nothing if this is the desired text): ')
if(isEmptyOrSpaces(desiredoutput)){
const good = [{
"text": input1,"camEmotion": input2,"tone": input3,"response": output
}]
console.log(good)
const data = fs.readFileSync('./tst.json',{encoding:'utf8',flag:'r'});
const array3 = good.concat(data)
fs.writeFileSync('tst.json',JSON.stringify(array3),null,4,"/t",JSON.stringify(array3).replace(/\\"/g,'"'));
}else{
let bad = {
text: input1,camEmotion: input2,tone: input3,response: desiredoutput
}
let data1 = JSON.stringify(bad);
fs.writeFileSync('test.json',data1);
}
run()
}
function isEmptyOrSpaces(desiredoutput){
return desiredoutput === null || desiredoutput.match(/^ *$/) !== null;
}
run()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。