LoginSignup
1
0

More than 5 years have passed since last update.

micropythonに置いたjsonファイルを読み込む方法

Posted at

M5Stack上に置いたjsonファイルをconfigの用に扱いたかった。
テスト用に以下のようなjsonとコードを用意してテストしました。

config.json
{
  "ssid":"test",
  "password":"password"
}

今回はampy pushでflash上においています。ujsonというライブラリに読み込んだ文字列を引き渡す感じで実装してみました。

main.py
import ujson
json = ujson.loads(open("config.json").read())
print(json)
print(json['ssid'])
print(json['password'])

上記のコードをampy runで動かしてみると下のような出力が得られます。

{'ssid': 'test', 'password': 'password'}
test
password

というわけでconfig.json等をsdに入れることで、環境を簡単に差し替えて使えるようにすることができそうです。

1
0
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
1
0