Saving JSON in Electron(在 Electron 中保存 JSON)
问题描述
我正在使用 Electron 构建一个应用程序.在这个应用程序中,我正在使用 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 缺乏一种简单的方法来持久化和读取应用程序的用户设置.electron-json-storage 实现了一个类似于 localStorage
的 API根据 app.getPath('userData')
的定义,在操作系统应用程序数据目录中写入和读取 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Electron 中保存 JSON
基础教程推荐
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01