示例
以下示例展示 JSON 转 CSV 转换器在不同场景下的实际用法。
基础示例
简单数组转换
输入 JSON:
[
{ "name": "Alice", "age": 28, "city": "New York" },
{ "name": "Bob", "age": 32, "city": "Los Angeles" },
{ "name": "Charlie", "age": 25, "city": "Chicago" }
]
输出 CSV:
name,age,city
Alice,28,New York
Bob,32,Los Angeles
Charlie,25,Chicago
单个对象转换
输入 JSON:
{
"product": "Laptop",
"price": 1299.99,
"category": "Electronics",
"inStock": true,
"tags": ["portable", "gaming", "work"]
}
输出 CSV:
product,price,category,inStock,tags
Laptop,1299.99,Electronics,true,"[""portable"",""gaming"",""work""]"
真实场景示例
电商商品数据
输入 JSON(商品目录):
[
{
"id": "P001",
"name": "Wireless Headphones",
"brand": "TechSound",
"price": 199.99,
"currency": "USD",
"availability": {
"inStock": true,
"quantity": 150
},
"specifications": {
"battery": "20 hours",
"connectivity": "Bluetooth 5.0",
"weight": "250g"
},
"reviews": {
"average": 4.5,
"count": 128
}
},
{
"id": "P002",
"name": "Smart Watch",
"brand": "FitTech",
"price": 299.99,
"currency": "USD",
"availability": {
"inStock": false,
"quantity": 0
},
"specifications": {
"battery": "7 days",
"connectivity": "Bluetooth, WiFi",
"weight": "45g"
},
"reviews": {
"average": 4.2,
"count": 89
}
}
]
输出 CSV(拍平后):
id,name,brand,price,currency,availability.inStock,availability.quantity,specifications.battery,specifications.connectivity,specifications.weight,reviews.average,reviews.count
P001,Wireless Headphones,TechSound,199.99,USD,true,150,20 hours,Bluetooth 5.0,250g,4.5,128
P002,Smart Watch,FitTech,299.99,USD,false,0,7 days,"Bluetooth, WiFi",45g,4.2,89
API 响应数据
输入 JSON(用户管理 API):
{
"users": [
{
"id": 1,
"username": "john_doe",
"email": "john@example.com",
"profile": {
"firstName": "John",
"lastName": "Doe",
"avatar": "https://example.com/avatars/john.jpg"
},
"permissions": ["read", "write"],
"lastLogin": "2024-01-15T10:30:00Z",
"active": true
},
{
"id": 2,
"username": "jane_smith",
"email": "jane@example.com",
"profile": {
"firstName": "Jane",
"lastName": "Smith",
"avatar": "https://example.com/avatars/jane.jpg"
},
"permissions": ["read"],
"lastLogin": "2024-01-14T15:45:00Z",
"active": true
}
],
"total": 2,
"page": 1,
"limit": 10
}
输出 CSV(仅 users 数组):
id,username,email,profile.firstName,profile.lastName,profile.avatar,permissions,lastLogin,active
1,john_doe,john@example.com,John,Doe,https://example.com/avatars/john.jpg,"[""read"",""write""]",2024-01-15T10:30:00Z,true
2,jane_smith,jane@example.com,Jane,Smith,https://example.com/avatars/jane.jpg,"[""read""]",2024-01-14T15:45:00Z,true
高级示例
金融数据
输入 JSON(股票市场数据):
[
{
"symbol": "AAPL",
"company": "Apple Inc.",
"sector": "Technology",
"price": {
"current": 175.43,
"open": 174.2,
"high": 176.15,
"low": 173.5,
"previousClose": 174.2
},
"volume": 45678900,
"marketCap": 2750000000000,
"dividend": {
"yield": 0.0044,
"amount": 0.24,
"frequency": "quarterly"
},
"analysts": {
"rating": "Buy",
"targetPrice": 185.0,
"recommendations": {
"strongBuy": 12,
"buy": 8,
"hold": 3,
"sell": 1,
"strongSell": 0
}
}
}
]
输出 CSV:
symbol,company,sector,price.current,price.open,price.high,price.low,price.previousClose,volume,marketCap,dividend.yield,dividend.amount,dividend.frequency,analysts.rating,analysts.targetPrice,analysts.recommendations
AAPL,Apple Inc.,Technology,175.43,174.20,176.15,173.50,174.20,45678900,2750000000000,0.0044,0.24,quarterly,Buy,185.00,"{""strongBuy"":12,""buy"":8,""hold"":3,""sell"":1,""strongSell"":0}"
调查数据
输入 JSON(客户满意度调查):
[
{
"respondentId": "R001",
"timestamp": "2024-01-15T14:30:00Z",
"demographics": {
"age": "25-34",
"gender": "Female",
"location": "California"
},
"responses": {
"satisfaction": 4,
"recommendation": 5,
"comments": "Great product, excellent customer service!"
},
"metadata": {
"surveyVersion": "v2.1",
"completionTime": 180,
"device": "mobile"
}
},
{
"respondentId": "R002",
"timestamp": "2024-01-15T16:45:00Z",
"demographics": {
"age": "35-44",
"gender": "Male",
"location": "Texas"
},
"responses": {
"satisfaction": 3,
"recommendation": 3,
"comments": "Product is okay, but could be better."
},
"metadata": {
"surveyVersion": "v2.1",
"completionTime": 120,
"device": "desktop"
}
}
]
输出 CSV:
respondentId,timestamp,demographics.age,demographics.gender,demographics.location,responses.satisfaction,responses.recommendation,responses.comments,metadata.surveyVersion,metadata.completionTime,metadata.device
R001,2024-01-15T14:30:00Z,25-34,Female,California,4,5,"Great product, excellent customer service!",v2.1,180,mobile
R002,2024-01-15T16:45:00Z,35-44,Male,Texas,3,3,"Product is okay, but could be better.",v2.1,120,desktop
特殊字符处理
国际化数据
输入 JSON(多语言内容):
[
{
"id": 1,
"title": "Café & Restaurant",
"description": "Un café français avec une cuisine délicieuse",
"location": "Paris, France",
"specialChars": "Café, naïve, résumé, piñata, 北京, 東京"
},
{
"id": 2,
"title": "北京烤鸭店",
"description": "正宗北京烤鸭,传统工艺制作",
"location": "北京, 中国",
"specialChars": "中文,English,Français,Español"
}
]
输出 CSV(UTF-8 编码):
id,title,description,location,specialChars
1,Café & Restaurant,Un café français avec une cuisine délicieuse,Paris France,Café naïve résumé piñata 北京 東京
2,北京烤鸭店,正宗北京烤鸭传统工艺制作,北京 中国,中文EnglishFrançaisEspañol
含引号与逗号的数据
输入 JSON:
[
{
"name": "Smith, John",
"quote": "He said \"Hello, World!\" to everyone",
"description": "A person who likes to say \"Hello\" and \"Goodbye\"",
"address": "123 Main St, Apt 4B, New York, NY 10001"
}
]
输出 CSV(带引号转义):
name,quote,description,address
"Smith, John","He said ""Hello, World!"" to everyone","A person who likes to say ""Hello"" and ""Goodbye""","123 Main St, Apt 4B, New York, NY 10001"
配置示例
欧洲 CSV 格式
设置:
- 分隔符:分号 (;)
- 编码:UTF-8
- 包含表头:是
- 转义引号:是
输入 JSON:
[
{ "name": "Müller", "city": "München", "price": "1.234,56" },
{ "name": "Schmidt", "city": "Köln", "price": "2.345,67" }
]
输出 CSV:
name;city;price
Müller;München;1.234,56
Schmidt;Köln;2.345,67
制表符分隔值(TSV)
设置:
- 分隔符:Tab
- 编码:UTF-8
- 包含表头:是
- 转义引号:否
输入 JSON:
[
{ "gene": "BRCA1", "mutation": "c.5266dupC", "significance": "Pathogenic" },
{ "gene": "TP53", "mutation": "c.742C>T", "significance": "Likely Pathogenic" }
]
输出 TSV:
gene mutation significance
BRCA1 c.5266dupC Pathogenic
TP53 c.742C>T Likely Pathogenic
错误处理示例
无效 JSON 输入
输入(缺少逗号):
[
{"name": "John" "age": 30}
]
错误输出:
Invalid JSON format. Please check your input.
空数据
输入:
[]
输出:
(Empty CSV - no data to convert)
混合数据类型
输入 JSON:
[
{ "id": 1, "name": "Product A", "active": true, "price": 99.99, "tags": null },
{ "id": 2, "name": "Product B", "active": false, "price": 149.99, "tags": ["electronics"] },
{ "id": 3, "name": "Product C", "active": true, "price": null, "tags": ["gadget", "portable"] }
]
输出 CSV:
id,name,active,price,tags
1,Product A,true,99.99,
2,Product B,false,149.99,"[""electronics""]"
3,Product C,true,,"[""gadget"",""portable""]"
性能示例
大型数据集
输入 JSON(1000 条记录):
[
// ... 1000 user records
{
"id": 1,
"name": "User 1",
"email": "user1@example.com",
"department": "Engineering",
"salary": 75000,
"hireDate": "2020-01-15"
}
// ... 999 more records
]
处理时间:约 50ms(1000 条记录)
输出大小:约 150KB CSV 文件
内存占用:处理期间约 2MB
内存优化
对于超大数据集,建议分块处理:
// Process 10,000 records in chunks of 1000
const chunkSize = 1000;
const chunks = [];
for (let i = 0; i < largeDataset.length; i += chunkSize) {
chunks.push(largeDataset.slice(i, i + chunkSize));
}
// Process each chunk
chunks.forEach((chunk, index) => {
const csv = jsonToCSV(chunk);
downloadCSV(csv, `data_chunk_${index + 1}.csv`);
});