-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
191 lines (165 loc) · 5.99 KB
/
index.html
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
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Online</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.button-group {
margin: 20px 0;
}
button {
padding: 10px 20px;
margin-right: 10px;
border: none;
border-radius: 4px;
background-color: #4CAF50;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
.json-pair {
display: flex;
margin: 10px 0;
padding: 10px;
background-color: #f8f9fa;
border-radius: 4px;
}
.key {
font-weight: bold;
margin-right: 10px;
min-width: 150px;
}
.value {
flex-grow: 1;
}
#jsonInput {
width: 100%;
height: 100px;
margin: 10px 0;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
#preview {
white-space: pre-wrap;
background-color: #f8f9fa;
padding: 10px;
border-radius: 4px;
margin-top: 10px;
}
/* 新增样式:隐藏Key时的类 */
.hidden-key .key {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>JSON在线工具</h1>
<div class="button-group">
<input type="file" id="fileInput" accept=".json" style="display: none;">
<button onclick="document.getElementById('fileInput').click()">导入JSON</button>
<button id="toggleKeyBtn" onclick="toggleKeyVisibility()">隐藏Key</button>
<button onclick="combineJSON()">拼接JSON</button>
<button onclick="exportJSON()">导出JSON</button>
</div>
<div id="jsonContainer"></div>
<h3>预览:</h3>
<div id="preview"></div>
</div>
<script>
let originalJson = {};
let keysHidden = false;
document.getElementById('fileInput').addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
try {
originalJson = JSON.parse(e.target.result);
displayJSON(originalJson);
} catch (error) {
alert('JSON格式错误!');
}
};
reader.readAsText(file);
});
function displayJSON(json) {
const container = document.getElementById('jsonContainer');
container.innerHTML = '';
Object.entries(json).forEach(([key, value]) => {
const div = document.createElement('div');
div.className = 'json-pair';
div.innerHTML = `
<span class="key" data-original-key="${key}">${key}:</span>
<span class="value">${value}</span>
`;
container.appendChild(div);
});
// 如果当前Key是隐藏的,确保新加载的内容也隐藏
if (keysHidden) {
container.classList.add('hidden-key');
document.getElementById('toggleKeyBtn').textContent = '显示Key';
} else {
container.classList.remove('hidden-key');
document.getElementById('toggleKeyBtn').textContent = '隐藏Key';
}
}
function combineJSON() {
const keys = Array.from(document.querySelectorAll('.key'))
.map(k => k.getAttribute('data-original-key'));
const values = Array.from(document.querySelectorAll('.value'))
.map(v => v.textContent.trimStart()); // 去除开头的空格
const newJson = {};
keys.forEach((key, index) => {
newJson[key] = values[index];
});
document.getElementById('preview').textContent =
JSON.stringify(newJson, null, 2);
}
function exportJSON() {
const jsonStr = document.getElementById('preview').textContent;
if (!jsonStr) {
alert('请先点击拼接JSON按钮生成预览!');
return;
}
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'exported.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function toggleKeyVisibility() {
const container = document.getElementById('jsonContainer');
const toggleBtn = document.getElementById('toggleKeyBtn');
keysHidden = !keysHidden;
if (keysHidden) {
container.classList.add('hidden-key');
toggleBtn.textContent = '显示Key';
} else {
container.classList.remove('hidden-key');
toggleBtn.textContent = '隐藏Key';
}
}
</script>
</body>
</html>