吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1654|回复: 14
收起左侧

[Python 原创] Json压缩和格式化工具

  [复制链接]
hrh123 发表于 2023-5-29 22:42
这几天在搞一个项目,有的时候要看一些json,网上找到了一些在线工具和插件,但还是觉得麻烦,毕竟pyhon本来就有个json的库,于是写了个,(因为是自用的,GUI巨丑,大家凑合看)
import json
import tkinter as tk

def json_compress(json_str):
    try:
        json_obj = json.loads(json_str)
    except Exception as e:
        print(e)
        return ""
    else:
        return json.dumps(json_obj, separators=(",", ":"))

def json_format(json_str):
    try:
        json_obj = json.loads(json_str)
    except Exception as e:
        print(e)
        return ""
    else:
        return json.dumps(json_obj, indent=4)

class GUI(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        master.title("JSON压缩和格式化工具")
        master.geometry("800x600")
        self.text = tk.Text(self)
        self.compress_button = tk.Button(
            self, text="压缩", command=self.compress)
        self.format_button = tk.Button(self, text="格式化", command=self.format)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.compress_button.pack(side=tk.LEFT)
        self.format_button.pack(side=tk.RIGHT)

    def compress(self):
        json_str = self.text.get(1.0, tk.END)
        compressed_str = json_compress(json_str)
        self.text.delete(1.0, tk.END)
        self.text.insert(1.0, compressed_str)

    def format(self):
        json_str = self.text.get(1.0, tk.END)
        formatted_str = json_format(json_str)
        self.text.delete(1.0, tk.END)
        self.text.insert(1.0, formatted_str)

root = tk.Tk()
app = GUI(root)
app.pack(fill=tk.BOTH, expand=True)
root.mainloop()

免费评分

参与人数 3吾爱币 +3 热心值 +2 收起 理由
jamessteed + 1 + 1 用心讨论,共获提升!
timeni + 1 + 1 用心讨论,共获提升!
Boly + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

TabKey9 发表于 2023-5-30 15:16
本帖最后由 TabKey9 于 2023-5-30 15:35 编辑

@hrh123 ,楼主看看给你加的UI还满意吗?

https://tabkey9.lanzoue.com/b00x1zxib
密码:52pj
蓝奏有打包好的 Windows可执行文件(10.2 MB)

1.png

2.png

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
hrh123 + 1 + 1 热心回复!

查看全部评分

806785900 发表于 2023-5-30 07:09
cao777 发表于 2023-5-30 08:11
本帖最后由 cao777 于 2023-5-30 08:30 编辑

学习了 不错的代码
头像被屏蔽
tlf 发表于 2023-5-30 08:52
提示: 作者被禁止或删除 内容自动屏蔽
ysjd22 发表于 2023-5-30 09:11
确实挺好的
chaozhi 发表于 2023-5-30 11:23
感谢分享,学习下tk和json
fengxiaoxiao7 发表于 2023-5-30 11:29
学习了,增加一个中文转译的功能就更好了
王家辉2010 发表于 2023-5-30 11:53
感谢楼主分享,支持一下!
TabKey9 发表于 2023-5-30 15:25
fengxiaoxiao7 发表于 2023-5-30 11:29
学习了,增加一个中文转译的功能就更好了

楼上有,支持中文
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 02:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表