Skip to content

Commit 293a2ec

Browse files
committed
增加json转csv工具
1 parent 4566bde commit 293a2ec

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎static/js/json-csv-converter.js‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const inputArea = document.getElementById('inputData');
2+
const outputArea = document.getElementById('outputData');
3+
const errorDisplay = document.getElementById('errorDisplay');
4+
5+
function clearError() {
6+
errorDisplay.textContent = '';
7+
}
8+
9+
function convertToCsv() {
10+
clearError();
11+
const jsonInput = inputArea.value.trim();
12+
if (!jsonInput) {
13+
errorDisplay.textContent = 'Please enter JSON data.';
14+
outputArea.value = '';
15+
return;
16+
}
17+
18+
try {
19+
const jsonData = JSON.parse(jsonInput);
20+
if (!Array.isArray(jsonData)) {
21+
errorDisplay.textContent = 'Input must be a JSON array of objects.';
22+
outputArea.value = '';
23+
return;
24+
}
25+
if (jsonData.length === 0) {
26+
outputArea.value = '';
27+
return;
28+
}
29+
30+
const headers = Object.keys(jsonData[0]);
31+
const csvRows = [];
32+
csvRows.push(headers.join(','));
33+
34+
for (const row of jsonData) {
35+
const values = headers.map(header => {
36+
const escaped = ('' + row[header]).replace(/"/g, '""');
37+
return `"${escaped}"`;
38+
});
39+
csvRows.push(values.join(','));
40+
}
41+
42+
outputArea.value = csvRows.join('\n');
43+
} catch (e) {
44+
console.error("JSON to CSV Error:", e);
45+
if (e instanceof SyntaxError) {
46+
errorDisplay.textContent = `JSON Parsing Error: ${e.message}`;
47+
} else {
48+
errorDisplay.textContent = `CSV Conversion Error: ${e.message}`;
49+
}
50+
outputArea.value = '';
51+
}
52+
}
53+
54+
function copyToClipboard() {
55+
if (!outputArea.value) return;
56+
navigator.clipboard.writeText(outputArea.value).then(() => {
57+
const icon = document.querySelector('.copy-icon');
58+
const originalPath = icon.innerHTML;
59+
icon.innerHTML = `<path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>`;
60+
setTimeout(() => {
61+
icon.innerHTML = originalPath;
62+
}, 2000);
63+
}).catch(err => {
64+
alert('无法复制到剪贴板: ' + err);
65+
});
66+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>JSON to CSV Converter</title>
7+
<link rel="stylesheet" href="../css/style.css">
8+
<style>
9+
.output-container {
10+
position: relative;
11+
}
12+
.copy-icon {
13+
position: absolute;
14+
top: 5px;
15+
right: 5px;
16+
cursor: pointer;
17+
width: 24px;
18+
height: 24px;
19+
fill: #888;
20+
}
21+
.copy-icon:hover {
22+
fill: #000;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<div class="tool-container tool-container-vertical">
28+
<div class="tool-header">
29+
<h1>JSON to CSV Converter</h1>
30+
<p>Convert data from JSON format to CSV format.</p>
31+
</div>
32+
33+
<div id="errorDisplay" class="tool-error"></div>
34+
35+
<div class="tool-io-section">
36+
<label for="inputData" class="tool-label">Input (JSON):</label>
37+
<textarea id="inputData" class="tool-textarea" placeholder="Paste JSON here..."></textarea>
38+
</div>
39+
40+
<div class="tool-controls">
41+
<button onclick="convertToCsv()" class="tool-button">Convert to CSV</button>
42+
</div>
43+
44+
<div class="tool-io-section">
45+
<label for="outputData" class="tool-label">Output (CSV):</label>
46+
<div class="output-container">
47+
<textarea id="outputData" class="tool-textarea" readonly placeholder="Conversion result will appear here..."></textarea>
48+
<svg class="copy-icon" onclick="copyToClipboard()" viewBox="0 0 24 24">
49+
<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>
50+
</svg>
51+
</div>
52+
</div>
53+
</div>
54+
55+
<script src="../js/json-csv-converter.js"></script>
56+
</body>
57+
</html>

‎tools.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@
140140
"path": "converter/json-yaml",
141141
"filePath": "static/tools/json-yaml-converter.html"
142142
},
143+
{
144+
"name_zh": "JSON转CSV",
145+
"name_en": "JSON to CSV Converter",
146+
"path": "converter/json-csv",
147+
"filePath": "static/tools/json-csv-converter.html"
148+
},
143149
{
144150
"name_zh": "进制转换",
145151
"name_en": "Base Converter",

0 commit comments

Comments
 (0)