Skip to content

Commit 92954fa

Browse files
committed
修复List嵌套泛型时由于错误的优化逻辑导致的异常代码;优化清理无用空行;更新测试例子并通过测试
1 parent 63d4231 commit 92954fa

File tree

7 files changed

+176
-77
lines changed

7 files changed

+176
-77
lines changed

Test/EmptyResp.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import 'dart:convert' show json;
22

3-
43
class EmptyResp {
54

65
Qwe qwe;
76

8-
97
EmptyResp.fromParams({this.qwe});
108

119
factory EmptyResp(jsonStr) => jsonStr is String ? EmptyResp.fromJson(json.decode(jsonStr)) : EmptyResp.fromJson(jsonStr);
1210

1311
EmptyResp.fromJson(jsonRes) {
1412
qwe = new Qwe.fromJson(jsonRes['qwe']);
15-
1613
}
1714

1815
@override
@@ -21,22 +18,28 @@ class EmptyResp {
2118
}
2219
}
2320

24-
25-
2621
class Qwe {
2722

2823
List<dynamic> asd;
2924
List<Object> qaz;
3025
List<dynamic> zxc;
3126

32-
3327
Qwe.fromParams({this.asd, this.qaz, this.zxc});
3428

3529
Qwe.fromJson(jsonRes) {
36-
asd = jsonRes['asd'];
37-
qaz = jsonRes['qaz'];
38-
zxc = jsonRes['zxc'];
30+
asd = [];
31+
32+
for (var asdItem in jsonRes['asd']){
33+
asd.add(asdItem);
34+
}
35+
36+
qaz = jsonRes['qaz'].cast<Object>();
37+
38+
zxc = [];
3939

40+
for (var zxcItem in jsonRes['zxc']){
41+
zxc.add(zxcItem);
42+
}
4043
}
4144

4245
@override

Test/ListsResp.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'dart:convert' show json;
2+
3+
class ListsResp {
4+
5+
List<List<List<int>>> asd;
6+
List<int> qaz;
7+
List<List<List<Zxc>>> qwe;
8+
9+
ListsResp.fromParams({this.asd, this.qaz, this.qwe});
10+
11+
factory ListsResp(jsonStr) => jsonStr is String ? ListsResp.fromJson(json.decode(jsonStr)) : ListsResp.fromJson(jsonStr);
12+
13+
ListsResp.fromJson(jsonRes) {
14+
asd = [];
15+
16+
for (var asdItem in jsonRes['asd']){
17+
List<List<int>> asdChild = [];
18+
for (var asdItemItem in asdItem){
19+
List<int> asdChildChild = [];
20+
for (var asdItemItemItem in asdItemItem){
21+
asdChildChild.add(asdItemItemItem);
22+
}
23+
asdChild.add(asdChildChild);
24+
}
25+
asd.add(asdChild);
26+
}
27+
28+
qaz = jsonRes['qaz'].cast<int>();
29+
30+
qwe = [];
31+
32+
for (var qweItem in jsonRes['qwe']){
33+
List<List<Zxc>> qweChild = [];
34+
for (var qweItemItem in qweItem){
35+
List<Zxc> qweChildChild = [];
36+
for (var qweItemItemItem in qweItemItem){
37+
qweChildChild.add(new Zxc.fromJson(qweItemItemItem));
38+
}
39+
qweChild.add(qweChildChild);
40+
}
41+
qwe.add(qweChild);
42+
}
43+
}
44+
45+
@override
46+
String toString() {
47+
return '{"asd": $asd,"qaz": $qaz,"qwe": $qwe}';
48+
}
49+
}
50+
51+
class Zxc {
52+
53+
int zxc;
54+
55+
Zxc.fromParams({this.zxc});
56+
57+
Zxc.fromJson(jsonRes) {
58+
zxc = jsonRes['zxc'];
59+
}
60+
61+
@override
62+
String toString() {
63+
return '{"zxc": $zxc}';
64+
}
65+
}
66+

Test/ListsTest.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"asd": [
3+
[
4+
[
5+
1
6+
]
7+
]
8+
],
9+
"qwe": [
10+
[
11+
[
12+
{
13+
"zxc": 1
14+
}
15+
]
16+
]
17+
],
18+
"qaz": [
19+
1
20+
]
21+
}

Test/RegionResp.dart

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import 'dart:convert' show json;
22

3-
43
class RegionResp {
54

65
int code;
76
int ttl;
87
String message;
98
Data data;
109

11-
1210
RegionResp.fromParams({this.code, this.ttl, this.message, this.data});
1311

1412
factory RegionResp(jsonStr) => jsonStr is String ? RegionResp.fromJson(json.decode(jsonStr)) : RegionResp.fromJson(jsonStr);
@@ -26,26 +24,21 @@ class RegionResp {
2624
}
2725
}
2826

29-
30-
3127
class Data {
3228

3329
List<Arch> archives;
3430
Page page;
3531

36-
3732
Data.fromParams({this.archives, this.page});
3833

3934
Data.fromJson(jsonRes) {
4035
archives = [];
4136

4237
for (var archivesItem in jsonRes['archives']){
43-
44-
archives.add(new Arch.fromJson(archivesItem));
38+
archives.add(new Arch.fromJson(archivesItem));
4539
}
4640

4741
page = new Page.fromJson(jsonRes['page']);
48-
4942
}
5043

5144
@override
@@ -54,22 +47,18 @@ class Data {
5447
}
5548
}
5649

57-
58-
5950
class Page {
6051

6152
int count;
6253
int num;
6354
int size;
6455

65-
6656
Page.fromParams({this.count, this.num, this.size});
6757

6858
Page.fromJson(jsonRes) {
6959
count = jsonRes['count'];
7060
num = jsonRes['num'];
7161
size = jsonRes['size'];
72-
7362
}
7463

7564
@override
@@ -78,8 +67,6 @@ class Page {
7867
}
7968
}
8069

81-
82-
8370
class Arch {
8471

8572
int aid;
@@ -100,7 +87,6 @@ class Arch {
10087
Rights rights;
10188
Stat stat;
10289

103-
10490
Arch.fromParams({this.aid, this.attribute, this.copyright, this.ctime, this.duration, this.pubdate, this.state, this.tid, this.videos, this.desc, this.dynamic, this.pic, this.title, this.tname, this.owner, this.rights, this.stat});
10591

10692
Arch.fromJson(jsonRes) {
@@ -121,7 +107,6 @@ class Arch {
121107
owner = new Owner.fromJson(jsonRes['owner']);
122108
rights = new Rights.fromJson(jsonRes['rights']);
123109
stat = new Stat.fromJson(jsonRes['stat']);
124-
125110
}
126111

127112
@override
@@ -130,8 +115,6 @@ class Arch {
130115
}
131116
}
132117

133-
134-
135118
class Stat {
136119

137120
int aid;
@@ -145,7 +128,6 @@ class Stat {
145128
int share;
146129
int view;
147130

148-
149131
Stat.fromParams({this.aid, this.coin, this.danmaku, this.favorite, this.his_rank, this.like, this.now_rank, this.reply, this.share, this.view});
150132

151133
Stat.fromJson(jsonRes) {
@@ -159,7 +141,6 @@ class Stat {
159141
reply = jsonRes['reply'];
160142
share = jsonRes['share'];
161143
view = jsonRes['view'];
162-
163144
}
164145

165146
@override
@@ -168,8 +149,6 @@ class Stat {
168149
}
169150
}
170151

171-
172-
173152
class Rights {
174153

175154
int bp;
@@ -180,7 +159,6 @@ class Rights {
180159
int no_reprint;
181160
int pay;
182161

183-
184162
Rights.fromParams({this.bp, this.download, this.elec, this.hd5, this.movie, this.no_reprint, this.pay});
185163

186164
Rights.fromJson(jsonRes) {
@@ -191,7 +169,6 @@ class Rights {
191169
movie = jsonRes['movie'];
192170
no_reprint = jsonRes['no_reprint'];
193171
pay = jsonRes['pay'];
194-
195172
}
196173

197174
@override
@@ -200,22 +177,18 @@ class Rights {
200177
}
201178
}
202179

203-
204-
205180
class Owner {
206181

207182
int mid;
208183
String face;
209184
String name;
210185

211-
212186
Owner.fromParams({this.mid, this.face, this.name});
213187

214188
Owner.fromJson(jsonRes) {
215189
mid = jsonRes['mid'];
216190
face = jsonRes['face'];
217191
name = jsonRes['name'];
218-
219192
}
220193

221194
@override

Test/WanResp.dart

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'dart:convert' show json;
22

3-
43
class WanResp {
54

65
int errorCode;
76
String errorMsg;
87
List<Data> data;
98

10-
119
WanResp.fromParams({this.errorCode, this.errorMsg, this.data});
1210

1311
factory WanResp(jsonStr) => jsonStr is String ? WanResp.fromJson(json.decode(jsonStr)) : WanResp.fromJson(jsonStr);
@@ -18,11 +16,8 @@ class WanResp {
1816
data = [];
1917

2018
for (var dataItem in jsonRes['data']){
21-
22-
data.add(new Data.fromJson(dataItem));
19+
data.add(new Data.fromJson(dataItem));
2320
}
24-
25-
2621
}
2722

2823
@override
@@ -31,8 +26,6 @@ class WanResp {
3126
}
3227
}
3328

34-
35-
3629
class Data {
3730

3831
int courseId;
@@ -43,7 +36,6 @@ class Data {
4336
String name;
4437
List<Children> children;
4538

46-
4739
Data.fromParams({this.courseId, this.id, this.order, this.parentChapterId, this.visible, this.name, this.children});
4840

4941
Data.fromJson(jsonRes) {
@@ -56,11 +48,8 @@ class Data {
5648
children = [];
5749

5850
for (var childrenItem in jsonRes['children']){
59-
60-
children.add(new Children.fromJson(childrenItem));
51+
children.add(new Children.fromJson(childrenItem));
6152
}
62-
63-
6453
}
6554

6655
@override
@@ -69,8 +58,6 @@ class Data {
6958
}
7059
}
7160

72-
73-
7461
class Children {
7562

7663
int courseId;
@@ -81,7 +68,6 @@ class Children {
8168
String name;
8269
List<dynamic> children;
8370

84-
8571
Children.fromParams({this.courseId, this.id, this.order, this.parentChapterId, this.visible, this.name, this.children});
8672

8773
Children.fromJson(jsonRes) {
@@ -91,8 +77,11 @@ class Children {
9177
parentChapterId = jsonRes['parentChapterId'];
9278
visible = jsonRes['visible'];
9379
name = jsonRes['name'];
94-
children = jsonRes['children'];
80+
children = [];
9581

82+
for (var childrenItem in jsonRes['children']){
83+
children.add(childrenItem);
84+
}
9685
}
9786

9887
@override

0 commit comments

Comments
 (0)