Ejemplo n.º 1
0
        public static Json CsvToJson(string csvSrc)
        {
            Json json = null;

            if (CSV.IsSrcValid(csvSrc))
            {
                CSV csv = CSV.Load(csvSrc);

                string[] fields = csv.Lines.First().Values;

                CsvLine[] lines = csv.Lines;

                json = new Json();

                for (int x = 1; x < lines.Length; x++)
                {
                    JsonObject obj = new JsonObject();

                    for (int y = 0; y < fields.Length; y++)
                    {
                        JsonKeyValue keyValue = new JsonKeyValue(fields[y], lines[x].Values[y]);
                        obj.AddKeyValue(keyValue);
                    }

                    json.AddObject(obj);
                }
            }

            return(json);
        }
        private void menuOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Arquivos de Dados(txt, csv, json) | *.txt; *.csv; *.json";
            dlg.Title  = "Choose data file";

            if (dlg.ShowDialog(this) == true)
            {
                string targeFile = File.ReadAllText(dlg.FileName);

                if (!String.IsNullOrEmpty(targeFile))
                {
                    if (CSV.IsSrcValid(targeFile))
                    {
                        json = null;
                        csv  = null;

                        csv = CSV.Load(targeFile);

                        ClearBoxesAndDisableButtons();
                        PopulateBoxes();
                    }
                    else if (Json.IsSrcValid(targeFile))
                    {
                        json = null;
                        csv  = null;

                        json = Json.Load(targeFile);

                        ClearBoxesAndDisableButtons();
                        PopulateBoxes();
                    }
                    else
                    {
                        MessageBox.Show("The file is not valid.", "ERROR!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }