三歩あるけば物も忘れる

お腹のお肉がメタボックル

ユーザ用ツール

サイト用ツール


DevelopmentLanguage:Python:Json01

書きかけ10.Json(Python)

なんとなくJsonのキーを取り出してみた

見出しレベル2

ここに見出しレベル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)

DevelopmentLanguage/Python/Json01.txt · 最終更新: 2023/01/08 by admin