コンテンツにスキップ

Python jsonファイルを読み込む

Usage

{
    "key1": "AAA",
    "key2": "BBB"
}
import json

json_open = open('./config/config.json', 'r')
json_load = json.load(json_open)

print(json_load) # {'key1': 'AAA', 'key2': 'BBB'}
print(json_load['key1']) # AAA

jsonが多層の場合

{
    "key1":{
        "name": "Tom",
        "number": 1
    },

    "key2":{
        "name": "Bob",
        "number": 2
    }
}

import json

json_open = open('./config/config.json', 'r')
json_load = json.load(json_open)

print(json_load['key1']['name']) # Tom

Reference