<table class="html5">
<tr class="li1"><td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 > > > > > > > > > > v-model><> > > : readonly><> > > > > > ><> score.js/1.8.3/underscore-min.js"><> > window.Json2Ts = (function () { function Json2Ts() { } Json2Ts.prototype.convert = function (content) { var jsonContent = JSON.parse(content); if (_.isArray(jsonContent)) { return this.convertObjectToTsInterfaces(jsonContent[0]); } return this.convertObjectToTsInterfaces(jsonContent); }; Json2Ts.prototype.convertObjectToTsInterfaces = function (jsonContent,objectName) { if (objectName === void 0) { objectName = "RootObject"; } var optionalKeys = []; var objectResult = []; for (var key in jsonContent) { var value = jsonContent[key]; if (_.isObject(value) objectResult.push(this.convertObjectToTsInterfaces(value,childObjectName)); jsonContent[key] = this.removeMajority(childObjectName) + ";"; } else if (_.isArray(value)) { var arrayTypes = this.detectMultiArrayTypes(value); if (this.isMultiArray(arrayTypes)) { var multiArrayBrackets = this.getMultiArrayBrackets(value); if (this.isAllEqual(arrayTypes)) { jsonContent[key] = arrayTypes[0].replace("[]",multiArrayBrackets); } else { jsonContent[key] = "any" + multiArrayBrackets + ";"; } } else if (value.length > 0 objectResult.push(this.convertObjectToTsInterfaces(value[0],childObjectName)); jsonContent[key] = this.removeMajority(childObjectName) + "[];"; } else { jsonContent[key] = arrayTypes[0]; } } else if (_.isDate(value)) { jsonContent[key] = "Date;"; } else if (_.isString(value)) { jsonContent[key] = "string;"; } else if (_.isBoolean(value)) { jsonContent[key] = "boolean;"; } else if (_.isNumber(value)) { jsonContent[key] = "number;"; } else { jsonContent[key] = "any;"; optionalKeys.push(key); } } var result = this.formatCharsToTypeScript(jsonContent,objectName,optionalKeys); objectResult.push(result); return objectResult.join("\n\n"); }; Json2Ts.prototype.detectMultiArrayTypes = function (value,valueType) { if (valueType === void 0) { valueType = []; } if (_.isArray(value)) { if (value.length === 0) { valueType.push("any[];"); } else if (_.isArray(value[0])) { for (var index = 0,length_1 = value.length; index index; this.detectMultiArrayTypeselement,; .concatvalueTypeResult; _.all,_.isString .push; _.all,_.isNumber .push; _.all,_.isBoolean .push; .push; ; ; function arrayTypes 1; }; Json2Ts.prototype.isAllEqual = function (array) { return _.all(array.slice(1),_.partial(_.isEqual,array[0])); }; Json2Ts.prototype.getMultiArrayBrackets = function (content) { var jsonString = JSON.stringify(content); var brackets = ""; for (var index = 0,length_2 = jsonString.length; index jsonStringindex; element brackets + ; length_2; ; function jsonContent,optionalKeys JSON.stringifyjsonContent,null, new RegExpg , :?: :: export interface IES y S > const app = new Vue({ el: '#app', data() { return { txt: '' } }, computed: { result() { try { return new Json2Ts().convert(this.txt) } catch (err) { return '' } } } }) > > > 原文链接:https://www.f2er.com/json/416616.html