@@ -87,15 +87,15 @@ def change_background_color(com_box, current_text):
8787
8888
8989# 生成字段类型下拉框
90- def get_type_combobox (line ):
90+ def get_type_combobox (need_connect , line ):
9191 com_box = QtWidgets .QComboBox ()
9292 com_box .setEditable (True )
9393 obj_type = int (line [- 1 ]) if line [- 1 ].isdigit () else 0
9494 global last_list_com_box
9595
9696 com_box .currentTextChanged .connect (partial (change_background_color , com_box ))
9797
98- if last_list_com_box is not None :
98+ if need_connect and last_list_com_box is not None :
9999 com_box .currentTextChanged .connect (partial (change_text , last_list_com_box ))
100100 last_list_com_box = None
101101
@@ -134,12 +134,14 @@ def update_list(json_str):
134134 ui .tv_fields .setRowCount (len (res ))
135135
136136 pre_type_combobox = None
137+ pre_index = - 1
137138 ii = 0
138139 for i in range (len (res )):
139140 line = res [i ]
140141 assert isinstance (line , str )
141142 index = line .find ('<' )
142- temp_type_combobox = get_type_combobox (line )
143+ temp_type_combobox = get_type_combobox (index != pre_index , line )
144+ pre_index = index
143145 ui .tv_fields .setCellWidget (ii , 1 , temp_type_combobox )
144146 if temp_type_combobox .count () > 1 and pre_type_combobox is not None and pre_type_combobox .currentText ().startswith ('List' ):
145147 ui .tv_fields .setRowCount (ui .tv_fields .rowCount () - 1 )
@@ -175,8 +177,9 @@ def json_format():
175177 # 将格式化后的json字符串覆盖到文本编辑框中
176178 ui .te_json .setText (jformat (json_str .replace ('\n ' , '' )))
177179
180+ # 为了修复json中含有内容为空的对象,也就是有'{}'这种玩意时解析失败的问题,先预处理把'{}'全部替换成null
178181 # 根据json更新表格条目
179- update_list (json_str )
182+ update_list (json_str . replace ( '{}' , 'null' ) )
180183
181184 else :
182185 msg = QtWidgets .QMessageBox ()
@@ -236,22 +239,6 @@ def init_view():
236239def custom_ui ():
237240 init_view ()
238241 init_event ()
239- #
240- # json_str = '''
241- # {
242- # "x": {
243- # "e": [
244- # {}
245- # ],
246- # "q": [],
247- # "t": []
248- # }
249- # }
250- #
251- # '''
252- #
253- # ui.te_json.setText(json_str)
254- # ui.btn_format.click()
255242
256243
257244if __name__ == "__main__" :
0 commit comments