-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpringBootJavaApplication.java
66 lines (53 loc) · 2.42 KB
/
SpringBootJavaApplication.java
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
package com.example.springbootjava;
import com.jsoncsvbridge.csv.CsvCreator;
import com.jsoncsvbridge.csv.MergeCsvCreator;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import static com.jsoncsvbridge.factory.DefaultCsvCreatorFactory.*;
import java.io.File;
@SpringBootApplication
public class SpringBootJavaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJavaApplication.class, args);
}
}
@Component
class CsvCreatorRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
File outputDir = new File("csv_output");
outputDir.mkdirs();
String jsonOutputPath = new File(outputDir, "output_json.csv").getAbsolutePath();
String mergeJsonOutputPath = new File(outputDir, "output_merge_json.csv").getAbsolutePath();
// JSON to CSV
// 기능1. JSON 형식 문자열 CSV 파일로 변환
CsvCreator jsonCreator = generateCsv("json");
// JSON to CSV
String jsonInput = """
[
{"name": "Hyunho", "age": 30, "city": "New York"},
{"name": "Bob", "age": 25, "city": "Los Angeles"},
{"name": "Charlie", "age": 35, "city": "Chicago"}
]
""";
jsonCreator.createCsv(jsonInput, jsonOutputPath);
System.out.println("JSON to CSV conversion completed. File saved at: " + jsonOutputPath);
// 기능2. 2개 JSON 형식 문자열 CSV 파일로 변환
CsvCreator mergeJsonCreator = generateMergeCsv();
String data1 = """
[
{"name": "John", "age": 30, "city": "New York", "country": "USA"}
]
""";
String data2 = """
[
{"name": "Alice", "age": 25, "city": "London", "occupation": "Engineer"},
{"name": "Bob", "age": 35, "city": "Paris", "hobby": "Photography"}
]
""";
((MergeCsvCreator) mergeJsonCreator).createMergedCsv(data1, data2, mergeJsonOutputPath);
System.out.println("Merged JSON to CSV conversion completed. File saved at: " + mergeJsonOutputPath);
}
}