JSON格式化工具

JSON格式化

json数据一行显示,如何美观,层次显?少量json数据,可以通过文件工具快捷键进行格式化,那么有很多条json,每打开一个json就需要快捷键一次,很麻烦。
制作一个gooey工具框架,内部编写功能脚本,实现JSON格式化工具,批量对JSON文件进行格式化。

  • 解析json文件夹
  • 利用json进行读取和写入

构建Gooey框架

from gooey import Gooey, GooeyParser

@Gooey(program_name='实现JSON格式化')
def main():
    parser = GooeyParser(description='JSON格式化')
    parser.add_argument('in_path', widget='DirChooser', metavar='选择输入目录', help='json文件夹')

    args = parser.parse_args()

    in_path = args.in_path
    parse(json_list_path=in_path)


if __name__ == '__main__':
    main()

解析json文件夹

方法可参考:https://www.cnblogs.com/icpy/p/16556531.html

json格式化

import json

def parse(json_list_path):
    json_file_list = list_files_deep(json_list_path, suffix='.json')
    for file_path in json_file_list:
        with open(file_path, 'r', encoding='utf-8') as f:
            org_json = json.load(f)
        with open(file_path, 'w', encoding='utf-8') as wf:
            # indent格式化
            # ensure_ascii显示中文,不转码
            json.dump(org_json, wf, indent=4, ensure_ascii=False)
posted @ 2022-08-08 13:57  EthanChai  阅读(6878)  评论(0编辑  收藏  举报