Skip to content

Commit 3897c51

Browse files
committed
修复不可见字符导致的bug
1 parent be0789b commit 3897c51

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

formatter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def update_list(json_str):
183183
def json_format():
184184
# 从文本编辑框获取json字符串
185185
json_str = ui.te_json.toPlainText()
186-
if is_json(json_str.strip()):
186+
json_str = rm_invisible(json_str.strip())
187+
if is_json(json_str):
187188
# 将格式化后的json字符串覆盖到文本编辑框中
188189
ui.te_json.setText(jformat(json_str.replace('\n', '')))
189190

@@ -212,7 +213,7 @@ def generate_bean():
212213
QMessageBox().information(msg_box_ui, "警告", "发生错误", QMessageBox.Ok)
213214
return
214215
if res != '':
215-
ui.te_json.setText(res)
216+
ui.te_json.setText(rm_invisible(res.strip()))
216217

217218

218219
def str_to_camel_case(text):

tools.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
# @Author : DebuggerX
66

77
import json
8+
9+
from string import printable
10+
811
from PyQt5.QtWidgets import QLabel, QComboBox, QMessageBox
912

1013
from template_code import get_top_code_dict, get_list_code_loop
1114

1215
msg_box_ui = None
1316

1417

18+
def rm_invisible(string):
19+
return ''.join(char for char in string if char in printable)
20+
21+
1522
# 验证json字符串是否合法
16-
def is_json(myjson):
23+
def is_json(my_json):
1724
try:
18-
j = json.loads(myjson)
25+
j = json.loads(my_json)
1926
except ValueError:
2027
return False
2128
if type(j) in (list, dict):

0 commit comments

Comments
 (0)