なんとなくJsonのキーを取り出してみた
ここに見出しレベル2のテキスト(以下同様)
import json
import os
file_name = 'test.json'
dir_path = os.path.dirname(__file__)
file_path = os.path.join(dir_path, file_name)
row = 0
col = 0
indent = "\t"
def dict_object(json_object, row, col, indent):
for k, v in json_object.items():
if type(k) == type(str()):
print(indent * col, k)
else:
print(v)
if type(v) == type(list()):
col += 1
list_object(v, row, col, indent)
col -= 1
elif type(v) == type(dict()):
col += 1
dict_object(v, row, col, indent)
col -= 1
def list_object(json_object, row, col, indent):
for l in json_object:
if type(l) == type(dict()):
dict_object(l, row, col, indent)
elif type(l) == type(list()):
list_object(l, row, col, indent)
if __name__ == '__main__':
with open(file_path, 'r') as json_file:
json_object = json.load(json_file)
if type(json_object) == type(dict()):
dict_object(json_object, row, col, indent)
elif type(json_object) == type(list()):
list_object(json_object, row, col, indent)