88from PyQt5 .QtWidgets import QLabel , QComboBox , QMessageBox
99
1010# 验证json字符串是否合法
11- from template_code import get_top_code_dict
11+ from template_code import get_top_code_dict , get_list_code_loop
1212
1313msg_box_ui = None
1414
@@ -44,14 +44,6 @@ def check_level_type(t):
4444
4545
4646def list_code_loop (code , count , total , n , ct ):
47- template = r"""
48- ${list_count}${class_type}${>count} ${name}${current_child} = [];
49- for (var ${name}${current_items} in ${name}${parent_items}){
50- ${loop}
51- ${name}${current_child}.add(${name}${child_child});
52- }
53- """
54-
5547 list_count = 'List<' * (total - count )
5648 ab_count = '>' * (total - count )
5749
@@ -61,15 +53,7 @@ def list_code_loop(code, count, total, n, ct):
6153 current_items = 'Item' * (count + 1 )
6254 parent_items = 'Item' * count
6355
64- fragment = template \
65- .replace ('${list_count}' , list_count ) \
66- .replace ('${class_type}' , ct ) \
67- .replace ('${>count}' , ab_count ) \
68- .replace ('${name}' , n ) \
69- .replace ('${current_child}' , current_child ) \
70- .replace ('${child_child}' , child_child ) \
71- .replace ('${current_items}' , current_items ) \
72- .replace ('${parent_items}' , parent_items )
56+ fragment = get_list_code_loop (list_count , ct , ab_count , n , current_child , child_child , current_items , parent_items )
7357
7458 if code == '' :
7559 return fragment
@@ -175,12 +159,15 @@ def build_level_code(level_bean):
175159
176160
177161def generate_code (work_bean ):
178- # 如果顶级容器为list而不是dict,则先在外面包一层dict,并将list的别名传递为dict的类型,list则重命名为'list',
162+ is_list_top = False
163+
164+ # 如果顶级容器为list而不是dict,则先在外面包一层dict,并将list的别名传递为dict的类型,list则重命名为'list',并修改标志位供后面修改使用
179165 (l , f , t , n ) = work_bean [0 ]
180166 if t .startswith ('List<' ):
181167 work_bean [0 ][3 ] = 'list'
182168 work_bean [0 ][1 ] = 'json_list'
183169 work_bean .insert (0 , (- 2 , "" , n , "" ))
170+ is_list_top = True
184171
185172 build_level_code (work_bean )
186173
@@ -192,13 +179,15 @@ def generate_code(work_bean):
192179
193180 codes .clear ()
194181
195- # 最终修改,添加jsonStr解析为jsonRes代码
182+ # 如果顶级容器为list,需要修改第一次获取JSON对象的方式为直接获取
183+ if is_list_top :
184+ res = res .replace ('jsonRes[\' list\' ]' , 'jsonRes' , 1 )
196185
186+ # 最终修改,添加jsonStr解析为jsonRes代码
197187 bp = res .find ('(jsonRes) {' )
198188 return 'import \' dart:convert\' ;\n ' + res [:bp ] + '(jsonStr) {\n var jsonRes = JSON.decode(jsonStr);\n ' + res [bp + 11 :]
199189
200190
201-
202191def check_and_generate_code (bean ):
203192 work_bean = []
204193 global msg_box_ui
0 commit comments