-2

How can I read my JSON file in my program and load it into my list ?

This is what my "load" method looks like. My program can also load and display csv file therefore in my load method is also code from csv.

public void Load(string path)
{
    Console.WriteLine("Dateiname eingeben, um zu laden.");
    string filename = Console.ReadLine();

    string json = ".json";
    bool o = filename.Contains(json);

    string fullFilePathName = System.IO.Path.Combine(path, filename);
    string fContent = File.ReadAllText(fullFilePathName);

    // Bremen;1200;1.2t\r\nHamburg;500;500kg\r\nBochum;5000;10kg
    liste.Clear();
    Console.WriteLine(fContent);
    string[] datensaetze = fContent.Split("\r\n");
    foreach (string datera in datensaetze)
    {
        if (datera.Length > 0)
        {
            // Format: Bremen;1200;1.2t
            LKW lkw = new LKW();
            if (lkw.Eingabe_CSV(datera) == true)
            {
                liste.Add(lkw);

            }

            else
            {

            }

YLR
  • 1,503
  • 4
  • 21
  • 28

1 Answers1

0

Check out: Reading CSV files using C# for csv files and System.Text.Json for json serialization/deserialization.

If you just want to split a string of semicolon separated values into a list you can use something like:

string data = "Bremen;1200;1.2t\r\nHamburg;500;500kg\r\nBochum;5000;10kg";
var aList = data.Split(';').ToList();
jhambright
  • 207
  • 1
  • 13