Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing to a JSON File and reading from it

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);


                    }

                });
            }
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading