You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
import json, re
|
|
|
|
def format_json(text):
|
|
try:
|
|
obj = json.loads(text)
|
|
return json.dumps(obj, indent=2, ensure_ascii=False)
|
|
except Exception:
|
|
return "JSON 格式错误,请检查输入"
|
|
|
|
def minify_json(text):
|
|
try:
|
|
obj = json.loads(text)
|
|
return json.dumps(obj, separators=(',', ':'))
|
|
except Exception:
|
|
return "JSON 格式错误,请检查输入"
|
|
|
|
def validate_json(text):
|
|
try:
|
|
json.loads(text)
|
|
return "✅ 有效的 JSON"
|
|
except Exception as e:
|
|
return f"❌ 格式错误: {str(e)}"
|
|
|
|
def page():
|
|
from flask import render_template, request
|
|
result = None
|
|
action = request.args.get("action")
|
|
text = request.args.get("text", "").strip()
|
|
|
|
if text:
|
|
if action == "format":
|
|
result = format_json(text)
|
|
elif action == "minify":
|
|
result = minify_json(text)
|
|
elif action == "validate":
|
|
result = validate_json(text)
|
|
else:
|
|
result = format_json(text)
|
|
else:
|
|
result = None
|
|
|
|
return render_template("json.html", result=result, text=text) |