LoginSignup
4
9

More than 5 years have passed since last update.

ElectronでローカルのJSONファイルを読み込んで変数に代入する方法

Posted at

わけあってローカルデータ管理をJSONでやっているわけですが、XMLHttpRequestとかに翻弄されながらさまよってましたがやっと解決策を。

Electronのときはfs(ファイルシステムモジュール)便利。ローカルのJSONひとつ読み込むのに時間を使いすぎてしまった。

var data = JSON.parse(fs.readFileSync('hoge/hoge/hoge.json', 'utf8'));

コンソールに呼び出して確認を。

console.log(data);

これでJSONを変数に渡すことができました。
のでHTMLへパースしたり、煮るなり焼くなりなんでもいけますね。

最初「javascript json parse」とか「local json parse」とかググってたけど、意外と「node json perse」とかであっさり見つかることがある。Node.js便利。

4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9