示例#1
0
        public static CSV Load(string srcText, char delimeter = ',')
        {
            if (IsSrcValid(srcText))
            {
                List <CsvLine> lines    = new List <CsvLine>();
                string[]       srcLines = srcText.Split('\n');

                foreach (string l in srcLines)
                {
                    lines.Add(CsvLine.Load(l.Trim(), delimeter));
                }

                CSV csv = new CSV();
                csv.lines.AddRange(lines);

                return(csv);
            }
            return(null);
        }
        private void btnJsonConvert_Click(object sender, RoutedEventArgs e)
        {
            if (txtRichJson.Document.Blocks.Count > 0)
            {
                TextRange textRange = new TextRange(txtRichJson.Document.ContentStart, txtRichJson.Document.ContentEnd);

                string jsonSrc = textRange.Text;

                if (Json.IsSrcValid(jsonSrc))
                {
                    json = Json.Load(jsonSrc);

                    csv = Converter.JsonToCsv(json);

                    ClearBoxesAndDisableButtons();
                    PopulateBoxes();
                }
            }
        }
示例#3
0
        public static CSV JsonToCsv(string jsonSrc)
        {
            CSV csv = null;

            if (Json.IsSrcValid(jsonSrc))
            {
                Json json = Json.Load(jsonSrc);

                List <CsvLine> lines = new List <CsvLine>();

                CsvLine fields = new CsvLine();

                foreach (JsonKeyValue keyValue in json.Objects.First().KeyValues)
                {
                    fields.AddValue(keyValue.Key);

                    if (keyValue == json.Objects.First().KeyValues.Last())
                    {
                        lines.Add(fields);
                    }
                }

                foreach (JsonObject obj in json.Objects)
                {
                    CsvLine line = new CsvLine();

                    foreach (JsonKeyValue keyValue in obj.KeyValues)
                    {
                        line.AddValue(keyValue.Value.ToString());
                    }

                    lines.Add(line);
                }

                csv = new CSV();

                csv.AddLines(lines);
            }

            return(csv);
        }
        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);
                    }
                }
            }
        }