node.js读写json文件.docx - Word
const fs = require('fs')
//读取json
fs.readFile('ChatRecord.json','utf8',(err,data)=>{
    if(err){
        console.log('读取json失败')
        return
    }
    console.log('读取json成功',data)
    var jsondata = JSON.parse(data)
    conn.send(JSON.stringify(jsondata))
})

//写入json
var jsonContent = JSON.stringify(ChatRecord); 
fs.writeFile('ChatRecord.json',jsonContent,'utf8',function(err){
     if(err){
         console.log('聊天记录写入失败')
         return
     }
     console.log('JSON file has been saved')
})
//如果是追加内容到文件中应该使用appendFile
fs.appendFile('ChatRecord.json',jsonContent,'utf8',function(err){})
//如果每条文本需要换行,在插入的字符串后添加rn
fs.appendFile('ChatRecord.json',jsonContent+'rn','utf8',function(err){})
第1页,共1页
本文共0个字符
中文(中国)
辅助功能
文档日期2023-07-24 11:19:31