Home > Back-end >  Writing to a JSON File and reading from it
Writing to a JSON File and reading from it

Time:04-30

I need to save the information from an input page into a JSON File and output the information onto another page reading from the JSON File.
I've tried many things and what seemed to work for me is using the specialfolder localapplication data.
Now, I don't quite understand how I can output the information and also check if the data is even put in correctly.
I previously used StreamReader to output the information on the JSON file and then put it on a ListView but this doesn't work if I have the file in the specialfolder. It says "stream cant be null". The commented out code is the code I tried in previous attempts.
Code:
ListPageVM (Read Page)

private ObservableCollection<MainModel> data;
        public ObservableCollection<MainModel> Data
        {
            get { return data; }
            set { data = value; OnPropertyChanged(); }
        }
        public ListPageVM()
        {
            var assembly = typeof(ListPageVM).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream(Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "eintraege.json"/"SaveUp.Resources.eintraege.json"/));

            //var file = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "eintraege.json");

            using (var reader = new StreamReader(stream))
            {
                var json = reader.ReadToEnd();

                List<MainModel> dataList = JsonConvert.DeserializeObject<List<MainModel>>(json);
                data = new ObservableCollection<MainModel>(dataList);
            }
        }

MainPageVM (Write Page)

public Command Einfügen
        {
            get
            {
                return new Command(() =>
                {
                    // Data ins Json
                    _mainModels.Add(DModel);

                    Datum = DateTime.Now.ToString("dd.mm.yyyy");
                    //var assembly = typeof(ListPageVM).GetTypeInfo().Assembly;
                    //FileStream stream = new FileStream("SaveUp.Resources.eintraege.json", FileMode.OpenOrCreate, FileAccess.Write);
                    var file = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "eintraege.json");
                    //Stream stream = assembly.GetManifestResourceStream("SaveUp.Resources.eintraege.json");
                    if (!File.Exists(file))
                    {
                        File.Create(file);
                    }
                    
                    using (var writer = File.AppendText(file))
                    {
                        string data = JsonConvert.SerializeObject(_mainModels);
                        writer.WriteLine(data);


                    }

                });
            }
        }

CodePudding user response:

you are trying to read and write resources, not files. That won't work. Instead do this

var path = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "eintraege.json");

File.WriteAllText(path, myjson);

to read the data back

var json = File.ReadAllText(path);
  • Related