問(wèn)題描述
我正在使用 Electron 構(gòu)建一個(gè)應(yīng)用程序.在這個(gè)應(yīng)用程序中,我正在使用 JSON 構(gòu)建數(shù)據(jù)結(jié)構(gòu).我的數(shù)據(jù)結(jié)構(gòu)如下所示:
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"的文件中.我想將其保存到文件中,因?yàn)槲蚁朐谙麓螒?yīng)用程序啟動(dòng)時(shí)加載.我的挑戰(zhàn)是,我不知道如何保存數(shù)據(jù).事實(shí)上,我什至不確定應(yīng)該將文件保存在 哪里.我是否將其保存在與應(yīng)用程序相同的目錄中?或者我應(yīng)該使用一些跨平臺(tái)的方法嗎?
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?
目前,我有以下內(nèi)容:
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 保存到本地文件系統(tǒng)以供以后使用?
So, how / where do I save JSON to the local file system for use at a later time?
推薦答案
Electron 缺乏一種簡(jiǎn)單的方法來(lái)持久化和讀取應(yīng)用程序的用戶設(shè)置.electron-json-storage 實(shí)現(xiàn)了一個(gè)類似于 localStorage
的 API根據(jù) app.getPath('userData')
的定義,在操作系統(tǒng)應(yīng)用程序數(shù)據(jù)目錄中寫(xiě)入和讀取 JSON 對(duì)象.
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')
.
這篇關(guān)于在 Electron 中保存 JSON的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!