如何将json数据附加到文件中?

时间:2021-10-28 07:11:46

var jsonfile = require('jsonfile');
var data = require('./data.json');
var items = data.items;
data.items[items.length] = {
  msg: "A new message"
};



jsonfile.writeFile('./data.json', data, {
  spaces: 2
}, function(err) {
  console.error(err);
});
{
  "items": [ {
    "name": "Tom", "time": "7.4.2016 3:13PM", "msg": "message1"
  }
  ,
  {
    "name": "Tom", "time": "7.4.2016 3:13PM", "msg": "message2"
  }
  ,
  {
    "name": "Tom", "time": "7.4.2016 3:13PM", "msg": "message3"
  }
  ]
}

I need to maintain a json file whose data style looks like the code above using node.js. And I should continuously add objects into the "items" array. However, I have to load and save the whole bunch of data every time I want to add a new item, if I adopt the method in the code above.
So my question is:
Is there a method to manipulate the json file directly, with no trouble of loading and saving the whole data(just like dealing with a database system)?

我需要使用node.js维护一个json文件,其数据样式与上面的代码类似。我应该不断地将对象添加到“items”数组中。但是,如果我采用上面代码中的方法,每次要添加新项时,我都必须加载并保存整组数据。所以我的问题是:是否有一种方法可以直接操作json文件,没有加载和保存整个数据的麻烦(就像处理数据库系统一样)?

2 个解决方案

#1


1  

If you find yourself loading and saving a "whole bunch of data" and are looking for ways to append json, that's a sign you need a database.

如果您发现自己正在加载并保存“一大堆数据”并且正在寻找附加json的方法,那么这就是您需要数据库的标志。

There's the typical mongodb, etc... servers. But if your needs are simple you can also look at simple embedded DBs like NEDB. This (and other embedded dbs) are file based DBs and they've solved the problem for you. No reason to be 'like a database system'. Just use one :)

有典型的mongodb等服务器。但如果您的需求很简单,您还可以查看简单的嵌入式数据库,如NEDB。这个(和其他嵌入式数据库)是基于文件的数据库,他们已经为您解决了这个问题。没有理由像'数据库系统'。只需使用一个:)

Another possibility if you have to stay with a files approach is to write individual files or if that's too many files, then you could write "pages" of items. But the feasibility depends on your read patterns.

如果你不得不采用文件方法的另一种可能性是编写单个文件,或者如果文件太多,那么你可以编写项目的“页面”。但可行性取决于您的阅读模式。

But once again, a db is probably more appropriate if you're in this state asking these questions.

但是再一次,如果你在这个状态下问这些问题,db可能更合适。

#2


0  

You can't really, not as long as the file looks like that (meaning you are applying styling to the output). You might be able to easily append if you instead just had one "entry" per line (this is what you should get when you don't have spaces: 2), then you could just:

你不能真的,只要文件看起来像那样(意味着你正在将样式应用于输出)。如果您只是每行只有一个“条目”(这是你没有空格时应该得到的东西:2),你可以轻松追加,然后你可以:

fs.appendFile('./foo.json', JSON.stringify(obj) + '\n', callback);

to append without JSON.parse() and a subsequent JSON.stringify().

在没有JSON.parse()和后续JSON.stringify()的情况下追加。

#1


1  

If you find yourself loading and saving a "whole bunch of data" and are looking for ways to append json, that's a sign you need a database.

如果您发现自己正在加载并保存“一大堆数据”并且正在寻找附加json的方法,那么这就是您需要数据库的标志。

There's the typical mongodb, etc... servers. But if your needs are simple you can also look at simple embedded DBs like NEDB. This (and other embedded dbs) are file based DBs and they've solved the problem for you. No reason to be 'like a database system'. Just use one :)

有典型的mongodb等服务器。但如果您的需求很简单,您还可以查看简单的嵌入式数据库,如NEDB。这个(和其他嵌入式数据库)是基于文件的数据库,他们已经为您解决了这个问题。没有理由像'数据库系统'。只需使用一个:)

Another possibility if you have to stay with a files approach is to write individual files or if that's too many files, then you could write "pages" of items. But the feasibility depends on your read patterns.

如果你不得不采用文件方法的另一种可能性是编写单个文件,或者如果文件太多,那么你可以编写项目的“页面”。但可行性取决于您的阅读模式。

But once again, a db is probably more appropriate if you're in this state asking these questions.

但是再一次,如果你在这个状态下问这些问题,db可能更合适。

#2


0  

You can't really, not as long as the file looks like that (meaning you are applying styling to the output). You might be able to easily append if you instead just had one "entry" per line (this is what you should get when you don't have spaces: 2), then you could just:

你不能真的,只要文件看起来像那样(意味着你正在将样式应用于输出)。如果您只是每行只有一个“条目”(这是你没有空格时应该得到的东西:2),你可以轻松追加,然后你可以:

fs.appendFile('./foo.json', JSON.stringify(obj) + '\n', callback);

to append without JSON.parse() and a subsequent JSON.stringify().

在没有JSON.parse()和后续JSON.stringify()的情况下追加。