在js / node.js中编辑json? [英] Edit json in js/node.js?

查看:91
本文介绍了在js / node.js中编辑json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个从json文件中读取数据的nodejs应用程序。现在我想在js中编辑这个json文件(写入它)。我该怎么做呢 ?

I am working on an nodejs app which reads data from a json file. Now I want to edit this json file in js (write to it). How do I do this ?

这是我的js代码:`

var fs = require("fs")

//function for file input
function getFile(filename) {
  var data = fs.readFileSync(filename,"ascii")
  return data }

//parsing json
var jsonString = [getFile("File.json")]
var jsonObj = JSON.parse(jsonString)`


推荐答案

根据需要修改jsonObj,创建一个新对象或其他,然后写文件:

Modify the jsonObj as you want, create a new object or whatever, then write the file:

fs.writeFileSync("File.json", jsonData);

这将覆盖文件(如果存在),以便您编辑文件。

This will overwrite the file if it exists, so that way you edit the file.

这篇关于在js / node.js中编辑json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆