简体   繁体   中英

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


                    }

                });
            }
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM