更新json文件中的数据

时间:2018-08-01 13:02:28

标签: json node.js

我想更新Data.json文件中的值。

{
"A0_Water":"-306.3",
"A1_Water":"0.0",
"A2_Water":"0.0",
"A3_Water":"0.0",
"Barometer":"100.8",
"Bar_Offset":"0",
"Temp":"29.95",
"ip":"192.168.2.47",
"serial":"02:42:52:bf:82:27",
"A0_Sensor":"PS30A",
"A1_Sensor":"None",
"A2_Sensor":"None",
"A3_Sensor":"None",
"A0_Offset":"0",
"A1_Offset":"0",
"A2_Offset":"0",
"A3_Offset":"0"
}

我正在工作的代码: 我目前不使用var数据对象来更新Data.json文件

var data = {
'A0_Water': c_inh2o_A0,
'A1_Water': c_inh2o_A1,
'A2_Water': c_inh2o_A2,
'A3_Water': c_inh2o_A3,
'Barometer': b_mbar_corrected,
'Temp': b_temp
};

  var fileName = '/home/admin/reactwatertracker/src/var/Data.json';
  var file4 = require(fileName);
  var file5 = file4;
  var file6 = file4;
  var file7 = file4;
  var file8 = file4;
  var file9 = file4;

   file4['A0_Water'] = c_inh2o_A0;
   file5['A1_Water'] = c_inh2o_A1;
   file6['A2_Water'] = c_inh2o_A2;
   file7['A3_Water'] = c_inh2o_A3;
   file8['Temp'] =   b_temp;
   file9['Barometer'] = b_mbar_corrected;

   console.log('Writing to' + fileName);
   fs.writeFile(fileName, JSON.stringify(file4,file5,file6,file7,file8,file9, null, 2),  function (err) {
     if (err) return console.log(err);
     console.log (JSON.stringify(file4));
  });

0 个答案:

没有答案