-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickCreateJSONSchema.py
More file actions
117 lines (95 loc) · 3.52 KB
/
Copy pathQuickCreateJSONSchema.py
File metadata and controls
117 lines (95 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : jsonUtil.py
# @Author: Lcy
# @Date : 2018/8/16
# @Desc :
from tkinter import *
import json
class main_GUI():
def __init__(self):
self.window = Tk()
self.window.title('QA工具')
# self.window['bg'] = 'GhostWhite'
# self.window.attributes('-alpha', 1)
self.window.geometry('800x600')
# 添加按钮
self.json_button = Button(self.window,
text='json2jsonschema',
bg='green',
command=self.go_json)
self.json_button['bg'] = 'blue'
self.json_button.pack()
self.window.mainloop()
def go_json(self):
Json_GUI()
class Json_GUI():
def __init__(self):
self.window = Tk()
# 设置主窗口属性(窗口名, 大小, 位置, 背景色等)
self.window.title('JSON处理工具')
# window.geometry('800x600+50+50')
self.window['bg'] = 'GhostWhite'
self.window.attributes('-alpha', 1)
#添加两个文本框
self.input_text = Text(self.window)
self.input_text['bg'] = 'pink'
self.input_text.grid(row=0, column=0, sticky=W)
self.result_labe = Text(self.window)
self.result_labe['bg'] = 'pink'
self.result_labe.grid(row=0, column=2)
self.to_json_button = Button(self.window, text='to_json', bg='lightblue', width=10, command=self.to_json)
self.to_json_button.grid(row=1, column=1)
self.window.mainloop()
# def set_window(self, window):
def to_json(self):
self.result_labe.delete(0.0, END)
def to_jsonschema(json_data, result):
'''
递归生成jsonschema
:param json_data: json数据
:param result:
:return:
'''
if isinstance(json_data, dict):
is_null = True
result.append('{')
result.append("'type': 'object',")
result.append("'properties': {")
for k, v in json_data.items():
is_null = False
result.append("'%s':" % k)
to_jsonschema(v, result)
result.append(',')
if not is_null:
result.pop()
result.append('}')
result.append('}')
elif isinstance(json_data, list):
result.append('{')
result.append("'type': 'array',")
result.append("'items': ")
to_jsonschema(json_data[0], result)
result.append('}')
elif isinstance(json_data, int):
result.append("{")
result.append("'type': 'number'")
result.append('}')
elif isinstance(json_data, str):
result.append("{")
result.append("'type': 'string'")
result.append('}')
return "".join(result)
json_data = self.input_text.get(0.0, END).strip().replace("\n", "")
print(json_data)
result = []
try:
testdata = to_jsonschema(eval(json_data), result)
print(testdata)
params = eval(testdata)
print(params)
self.result_labe.insert(1.0, json.dumps(params, indent=4))
except Exception as e:
self.result_labe.insert(1.0, '输入的JSON数据有问题, 请检查')
# json_gui = Json_GUI()
main_gui = main_GUI()