Created
June 3, 2025 02:12
-
-
Save ThaiseFreitas/ea9b4492e771f6eea0d86089704f0526 to your computer and use it in GitHub Desktop.
transformar formato CSV em Json para API em Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package integraçãoDeSistemas; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Scanner; | |
public class ConversorCSVParaJson { | |
public static void main(String[] args) { | |
Locale.setDefault(Locale.US); | |
Scanner sc = new Scanner(System.in); | |
List<String> dadosCSV = new ArrayList<>(); | |
dadosCSV.add("4668c219-296d-40de-a073-99b85026e977,01-01 Visão geral do capítulo.mp4,222"); | |
dadosCSV.add("81be4133-f81a-443a-96f3-0c30d7460ab8,01-02 Algoritmos e Lógica de Programação.mp4,396"); | |
String json = converterCsvParaJson(dadosCSV); | |
System.out.println(json); | |
sc.close(); | |
} | |
public static String converterCsvParaJson(List<String> csvData) { | |
StringBuilder jsonBuilder = new StringBuilder(); | |
jsonBuilder.append("[\n"); | |
for (int i = 0; i < csvData.size(); i++) { | |
String linha = csvData.get(i); | |
String[] campos = linha.split(","); | |
String id = campos[0]; | |
String tituloOriginal = campos[1]; | |
String tituloSemNumero = tituloOriginal.substring(6); | |
String tituloFinal = tituloSemNumero.replace(".mp4", ""); | |
int duracao = Integer.parseInt(campos[2]); | |
jsonBuilder.append(" {\n"); | |
jsonBuilder.append(" \"id\": \"" + id + "\",\n"); | |
jsonBuilder.append(" \"title\": \"" + tituloFinal + "\",\n"); | |
jsonBuilder.append(" \"duration\": " + duracao + "\n"); | |
if (i < csvData.size() - 1) { | |
jsonBuilder.append(" },\n"); | |
} else { | |
jsonBuilder.append(" }\n"); | |
} | |
} | |
jsonBuilder.append("]"); | |
return jsonBuilder.toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment