在Electron中保存JSON [英] Saving JSON in Electron

查看:675
本文介绍了在Electron中保存JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用电子构建应用。在这个应用程序中,我正在使用JSON构建数据结构。我的数据结构如下:

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

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

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

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 where I 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?

当前,我有以下内容:

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.
} 

因此,如何/在何处将JSON保存到本地文件系统以供使用

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

推荐答案

电子缺乏一种简单的方法来保存和读取应用程序的用户设置。 electron-json-storage 实现了与 localStorage ,以根据 app.getPath('userData')的定义,向操作系统应用程序数据目录写入JSON对象,或从操作系统应用程序数据目录读取JSON对象。

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

这篇关于在Electron中保存JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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