diff --git a/AppConverterConsole/Converter.cs b/AppConverterConsole/Converter.cs index 6d5f707..24a721d 100644 --- a/AppConverterConsole/Converter.cs +++ b/AppConverterConsole/Converter.cs @@ -8,22 +8,12 @@ public static void JsonToCSV() Console.Clear(); // Pedir que seja informado na execução do programa + Console.WriteLine("\n"); Console.WriteLine("Informar o texto JSON"); + var texto_json = Console.ReadLine(); - if (texto_json == "") - { - Console.WriteLine("O campo está vazio. Preencha-o novamente!"); - Thread.Sleep(4000); - Converter.JsonToCSV(); - } - else if (!IsValidJson(texto_json)) - { - Console.Write("\n"); - Console.WriteLine("JSON no formato inválido. Preencha-o novamente!"); - Thread.Sleep(4000); - Converter.JsonToCSV(); - } + ValidateText.CheckCondition(texto_json); // 3. Como ler a informação JSON para realizar a manipulação? @@ -85,12 +75,14 @@ public static void JsonToCSV() count_corpo++; } + var item_temp = corpo[1].ToString(); + var cont_linha = 0; + foreach (var item in corpo) { if (item != null) { - textoCSV[0 + 1] += "\"" + texto_json?.Split("\"" + item + "\"")[1].Split("\"")[1] + "\","; - + textoCSV[cont_linha + 1] += "\"" + texto_json?.Split("\"" + item + "\"")[1].Split("\"")[1] + "\","; } } @@ -106,45 +98,12 @@ public static void JsonToCSV() } Console.WriteLine("\n"); - Console.WriteLine("Esse é o texto convertido em um formato CSV:"); + Console.WriteLine("-----------------------------------------------"); + Console.WriteLine("| Texto convertido em um formato CSV: |"); + Console.WriteLine("-----------------------------------------------"); Console.WriteLine("\n"); Console.WriteLine(resultado); Menu.LimparTela(); - - static bool IsValidJson(string jsonString) - { - int tamanho = 0; - bool sequencia = false; - - foreach (char c in jsonString) - { - switch (c) - { - case '{': - case '[': - if (!sequencia) - { - tamanho++; - } - break; - - case '}': - case ']': - if (!sequencia) - { - tamanho--; - } - break; - - case '"': - sequencia = !sequencia; - break; - } - } - - return tamanho == 0 && !sequencia; - } } - } } \ No newline at end of file diff --git a/AppConverterConsole/ValidateText.cs b/AppConverterConsole/ValidateText.cs new file mode 100644 index 0000000..ac8613d --- /dev/null +++ b/AppConverterConsole/ValidateText.cs @@ -0,0 +1,63 @@ +namespace AppConverterConsole +{ + public static class ValidateText + { + public static bool IsValidJson(string json_text) + { + int tamanho = 0; + bool sequencia = false; + + foreach (char c in json_text) + { + switch (c) + { + case '{': + case '[': + if (!sequencia) + { + tamanho++; + } + break; + case '}': + case ']': + if (!sequencia) + { + tamanho--; + } + break; + case '"': + sequencia = !sequencia; + break; + } + } + return tamanho == 0 && !sequencia; + } + + public static void CheckCondition(string json_text) + { + if (json_text.StartsWith("[{") || json_text.StartsWith("{")) + { + if (json_text == "") + { + Mensagem("O campo está vazio. Preencha-o novamente!"); + } + else if (!IsValidJson(json_text)) + { + Mensagem("JSON no formato inválido. Preencha-o novamente!"); + } + } + else + { + Console.Write("\n"); + Mensagem("JSON no formato inválido. Preencha-o novamente!"); + } + } + + public static void Mensagem(string text) + { + Console.WriteLine(text); + Thread.Sleep(4000); + Converter.JsonToCSV(); + } + } +} \ No newline at end of file diff --git a/RegrasDeNegocio.docx b/RegrasDeNegocio.docx index 9b11c1e..8322649 100644 Binary files a/RegrasDeNegocio.docx and b/RegrasDeNegocio.docx differ