8
8
from PyQt5 .QtWidgets import QLabel , QComboBox , QMessageBox
9
9
10
10
# 验证json字符串是否合法
11
- from template_code import get_top_code_dict
11
+ from template_code import get_top_code_dict , get_list_code_loop
12
12
13
13
msg_box_ui = None
14
14
@@ -44,14 +44,6 @@ def check_level_type(t):
44
44
45
45
46
46
def 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
-
55
47
list_count = 'List<' * (total - count )
56
48
ab_count = '>' * (total - count )
57
49
@@ -61,15 +53,7 @@ def list_code_loop(code, count, total, n, ct):
61
53
current_items = 'Item' * (count + 1 )
62
54
parent_items = 'Item' * count
63
55
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 )
73
57
74
58
if code == '' :
75
59
return fragment
@@ -175,12 +159,15 @@ def build_level_code(level_bean):
175
159
176
160
177
161
def 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',并修改标志位供后面修改使用
179
165
(l , f , t , n ) = work_bean [0 ]
180
166
if t .startswith ('List<' ):
181
167
work_bean [0 ][3 ] = 'list'
182
168
work_bean [0 ][1 ] = 'json_list'
183
169
work_bean .insert (0 , (- 2 , "" , n , "" ))
170
+ is_list_top = True
184
171
185
172
build_level_code (work_bean )
186
173
@@ -192,13 +179,15 @@ def generate_code(work_bean):
192
179
193
180
codes .clear ()
194
181
195
- # 最终修改,添加jsonStr解析为jsonRes代码
182
+ # 如果顶级容器为list,需要修改第一次获取JSON对象的方式为直接获取
183
+ if is_list_top :
184
+ res = res .replace ('jsonRes[\' list\' ]' , 'jsonRes' , 1 )
196
185
186
+ # 最终修改,添加jsonStr解析为jsonRes代码
197
187
bp = res .find ('(jsonRes) {' )
198
188
return 'import \' dart:convert\' ;\n ' + res [:bp ] + '(jsonStr) {\n var jsonRes = JSON.decode(jsonStr);\n ' + res [bp + 11 :]
199
189
200
190
201
-
202
191
def check_and_generate_code (bean ):
203
192
work_bean = []
204
193
global msg_box_ui
0 commit comments