javascript 在 Electron 中保存 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33289110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:19:32  来源:igfitidea点击:

Saving JSON in Electron

javascriptjsonelectron

提问by JQuery Mobile

I am building an app using Electron. In this app, I am building a data structure using JSON. My data structure looks like this:

我正在使用Electron构建一个应用程序。在这个应用程序中,我正在使用 JSON 构建数据结构。我的数据结构如下所示:

{
  items: [
    { id:1, name:'football' },
    { id:2, name:'soccer ball' },
    { id:3, name:'basketball' }
  ]
}

I want to save this JSON to a file called "data.json". I want to save it to a file because I want to load the next time the application starts. My challenge is, I do not know how to save the data. In fact, I'm not sure whereI should even save the file. Do I save it in the same directory as the app? Or is there some cross-platform approach I should use?

我想将此 JSON 保存到名为“data.json”的文件中。我想将它保存到一个文件中,因为我想在下次应用程序启动时加载。我的挑战是,我不知道如何保存数据。事实上,我什至不确定我应该在哪里保存文件。我是否将其保存在与应用程序相同的目录中?或者我应该使用一些跨平台的方法吗?

Currently, I have the following:

目前,我有以下几点:

saveClick: function() {
  var json = JSON.stringify(this.data);
  // assume json matches the JSON provided above.
  // Now, I'm not sure how to actually save the file.
} 

So, how / where do I save JSON to the local file system for use at a later time?

那么,如何/在哪里将 JSON 保存到本地文件系统以供以后使用?

采纳答案by Bruno

Electron lacks an easy way to persist and read user settings for your application. electron-json-storageimplements an API somehow similar to localStorageto write and read JSON objects to/from the operating system application data directory, as defined by app.getPath('userData').

Electron 缺乏一种简单的方法来持久化和读取应用程序的用户设置。Electron-json-storage实现了一个 API,类似于localStorage在操作系统应用程序数据目录中写入和读取 JSON 对象,如app.getPath('userData').

回答by Ran Yitzhaki

I wrote a simple library that you can use, with a simple interface, it also creates subdirectories and works with promises/callbacks. it will save the data into app.getPath("appData") as the root folder.

我写了一个简单的库,你可以使用它,它有一个简单的界面,它还创建子目录并使用承诺/回调。它将数据保存到 app.getPath("appData") 作为根文件夹。

https://github.com/ran-y/electron-storage

https://github.com/ran-y/electron-storage

Installation

安装

$ npm install --save electron-storage

$ npm install --save electron-storage

usage

用法

const storage = require('electron-storage');

const storage = require('electron-storage');

API

应用程序接口

storage.get(filePath, (err, data) => {
  if (err) {
    console.error(err)
      } else {
        console.log(data);
      }
    });

storage.get(filePath)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });

storage.set(filePath, data, (err) => {
      if (err) {
        console.error(err)
      }
    });

storage.set(filePath, data)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });

回答by Andrew Feldman

Electron uses node.js as its core. You can use the following:

Electron 使用 node.js 作为其核心。您可以使用以下内容:

var fs = require("fs");
read_file = function(path){
     return fs.readFileSync(path, 'utf8');
}

write_file = function(path, output){
    fs.writeFileSync(path, output);
}

For write_file(), you can either pass "document.txt"as the path and it will write it to the same directory the html file it was run from. You can also put in a full path like "C:/Users/usern/document.txt"and it will write to the specific location you want.

对于write_file(),您可以将其"document.txt"作为路径传递,它会将其写入与运行它的 html 文件相同的目录中。你也可以输入一个完整的路径"C:/Users/usern/document.txt",它会写入你想要的特定位置。

Also, you can choose any file extention you want, (ie. ".txt", ".js", ".json", etc.). You can even make up your own!

此外,您可以选择您想要的任何文件扩展名(即“.txt”、“.js”、“.json”等)。你甚至可以自己制作!