-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-formatter.html
More file actions
396 lines (350 loc) · 12 KB
/
json-formatter.html
File metadata and controls
396 lines (350 loc) · 12 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON 格式化 - IT 工具集</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
min-height: 100vh;
color: #fff;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 30px 20px;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 30px;
}
.back-link {
color: #00d9ff;
text-decoration: none;
font-size: 0.95rem;
display: flex;
align-items: center;
gap: 8px;
opacity: 0.8;
transition: opacity 0.2s;
}
.back-link:hover { opacity: 1; }
h1 {
font-size: 1.8rem;
font-weight: 400;
color: #00d9ff;
}
.toolbar {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
button {
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 0.95rem;
cursor: pointer;
transition: all 0.2s;
font-weight: 500;
}
.btn-primary {
background: linear-gradient(135deg, #00d9ff, #00b8d4);
color: #0f0c29;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(0, 217, 255, 0.3);
}
.btn-secondary {
background: rgba(255, 255, 255, 0.1);
color: #fff;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.btn-secondary:hover {
background: rgba(255, 255, 255, 0.15);
}
.editor-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
@media (max-width: 768px) {
.editor-container { grid-template-columns: 1fr; }
}
.editor-panel {
background: rgba(0, 0, 0, 0.3);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
overflow: hidden;
}
.panel-header {
padding: 15px 20px;
background: rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
display: flex;
justify-content: space-between;
align-items: center;
}
.panel-header h3 {
font-weight: 400;
font-size: 1rem;
color: #aaa;
}
.panel-header .status {
font-size: 0.85rem;
padding: 4px 12px;
border-radius: 20px;
}
.status.valid {
background: rgba(0, 255, 136, 0.2);
color: #00ff88;
}
.status.invalid {
background: rgba(255, 107, 107, 0.2);
color: #ff6b6b;
}
.status.empty {
background: rgba(255, 255, 255, 0.1);
color: #888;
}
textarea {
width: 100%;
height: 500px;
background: transparent;
border: none;
padding: 20px;
color: #e0e0e0;
font-family: 'Consolas', 'Monaco', 'Fira Code', monospace;
font-size: 14px;
line-height: 1.6;
resize: vertical;
}
textarea:focus {
outline: none;
}
textarea::placeholder {
color: #555;
}
#output {
color: #00ff88;
}
.error-output {
color: #ff6b6b !important;
}
.stats {
margin-top: 20px;
padding: 15px 20px;
background: rgba(0, 0, 0, 0.2);
border-radius: 8px;
display: flex;
gap: 30px;
font-size: 0.9rem;
color: #888;
}
.stats span {
color: #00d9ff;
}
.toast {
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: #00d9ff;
color: #0f0c29;
padding: 12px 30px;
border-radius: 8px;
font-weight: 500;
opacity: 0;
transition: all 0.3s;
z-index: 1000;
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<header>
<a href="index.html" class="back-link">← 返回工具集</a>
<h1>{ } JSON 格式化</h1>
<div style="width: 100px;"></div>
</header>
<div class="toolbar">
<button class="btn-primary" onclick="formatJson()">格式化</button>
<button class="btn-primary" onclick="compressJson()">压缩</button>
<button class="btn-secondary" onclick="copyOutput()">复制结果</button>
<button class="btn-secondary" onclick="clearAll()">清空</button>
<button class="btn-secondary" onclick="loadSample()">加载示例</button>
</div>
<div class="editor-container">
<div class="editor-panel">
<div class="panel-header">
<h3>输入 JSON</h3>
<span id="inputStatus" class="status empty">等待输入</span>
</div>
<textarea id="input" placeholder='粘贴 JSON 数据,例如: {"name":"张三","age":25,"skills":["Python","JavaScript"]}'></textarea>
</div>
<div class="editor-panel">
<div class="panel-header">
<h3>输出结果</h3>
<span id="outputStatus" class="status empty">-</span>
</div>
<textarea id="output" readonly placeholder="格式化结果将显示在这里..."></textarea>
</div>
</div>
<div class="stats">
<div>输入字符数: <span id="inputChars">0</span></div>
<div>输出字符数: <span id="outputChars">0</span></div>
<div>缩进层级: <span id="indentLevel">2 空格</span></div>
</div>
</div>
<div id="toast" class="toast"></div>
<script>
const input = document.getElementById('input');
const output = document.getElementById('output');
const inputStatus = document.getElementById('inputStatus');
const outputStatus = document.getElementById('outputStatus');
const inputChars = document.getElementById('inputChars');
const outputChars = document.getElementById('outputChars');
// 实时验证
input.addEventListener('input', () => {
inputChars.textContent = input.value.length;
validateJson();
});
function validateJson() {
const value = input.value.trim();
if (!value) {
inputStatus.textContent = '等待输入';
inputStatus.className = 'status empty';
return null;
}
try {
const obj = JSON.parse(value);
inputStatus.textContent = '✓ 有效 JSON';
inputStatus.className = 'status valid';
return obj;
} catch (e) {
inputStatus.textContent = '✗ 格式错误';
inputStatus.className = 'status invalid';
return null;
}
}
function formatJson() {
try {
const value = input.value.trim();
if (!value) {
showToast('请输入 JSON 数据');
return;
}
const obj = JSON.parse(value);
const formatted = JSON.stringify(obj, null, 2);
output.value = formatted;
output.classList.remove('error-output');
outputStatus.textContent = '格式化成功';
outputStatus.className = 'status valid';
outputChars.textContent = formatted.length;
} catch (e) {
output.value = '❌ JSON 格式错误\n\n' + e.message;
output.classList.add('error-output');
outputStatus.textContent = '解析失败';
outputStatus.className = 'status invalid';
}
}
function compressJson() {
try {
const value = input.value.trim();
if (!value) {
showToast('请输入 JSON 数据');
return;
}
const obj = JSON.parse(value);
const compressed = JSON.stringify(obj);
output.value = compressed;
output.classList.remove('error-output');
outputStatus.textContent = '压缩成功';
outputStatus.className = 'status valid';
outputChars.textContent = compressed.length;
} catch (e) {
output.value = '❌ JSON 格式错误\n\n' + e.message;
output.classList.add('error-output');
outputStatus.textContent = '解析失败';
outputStatus.className = 'status invalid';
}
}
function copyOutput() {
if (!output.value) {
showToast('没有可复制的内容');
return;
}
navigator.clipboard.writeText(output.value).then(() => {
showToast('已复制到剪贴板');
}).catch(() => {
output.select();
document.execCommand('copy');
showToast('已复制到剪贴板');
});
}
function clearAll() {
input.value = '';
output.value = '';
inputStatus.textContent = '等待输入';
inputStatus.className = 'status empty';
outputStatus.textContent = '-';
outputStatus.className = 'status empty';
inputChars.textContent = '0';
outputChars.textContent = '0';
}
function loadSample() {
const sample = {
"code": 200,
"message": "success",
"data": {
"user": {
"id": "U001",
"name": "张三",
"email": "zhangsan@example.com"
},
"orders": [
{"orderId": "ORD001", "amount": 299.00, "status": "completed"},
{"orderId": "ORD002", "amount": 599.00, "status": "pending"}
],
"metadata": {
"timestamp": 1704067200,
"version": "1.0.0"
}
}
};
input.value = JSON.stringify(sample);
inputChars.textContent = input.value.length;
validateJson();
showToast('已加载示例数据');
}
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
}, 2000);
}
// 快捷键支持
document.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey) {
if (e.key === 'Enter') {
e.preventDefault();
formatJson();
}
}
});
</script>
</body>
</html>