簡體   English   中英

當我嘗試讀取文件時,Json 讀取器異常

[英]Json reader exception when I try to read a file

在我的應用程序中,我可以插入注釋,並使用以下方法創建它:

public class Note
    {
        public string FraseGiorno { get; set; }
        public string Nota { get; set; }
        public string Data { get; set; }
    }  

string NotesFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Note.json");


ObservableCollection<Note> listNote = new ObservableCollection<Note>();

private async void Save_Clicked(object sender, EventArgs e)
    {
        await CreateNote.TranslateTo(0, 600, 500, Easing.SinIn);

        Note NoteNew = new Note();

        if (CheckBox.IsChecked = true)
        {
            NoteNew = new Note
            {
                FraseGiorno = obj1.FraseGiornaliera,
                Nota = TestoNota.Text,
                Data = DateTime.Today.ToString().Remove(10, 9),
            };
        }
        else
        {
            NoteNew = new Note
            {
                Nota = TestoNota.Text,
                Data = DateTime.Today.ToString().Remove(10, 9),
            };
        }

        using (StreamWriter file = File.CreateText(NotesFile))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(file, NoteNew);
        }

        listNote.Add(NoteNew);

    }

當用戶移動到另一個頁面時,他可以閱讀他的筆記:

if (File.Exists(NotesFile))
        {
            var Note = JsonConvert.DeserializeObject<Note>(File.ReadAllText(NotesFile));
        }

這里拋出一個異常,但我不明白為什么

Newtonsoft.Json.JsonReaderException: '完成讀取 JSON 內容后遇到的附加文本:{. 路徑'',第 1 行,position 34。 塊引用

在此處輸入圖像描述

從共享的字符串數據中,您需要用方括號將其括起來,這表示它是一個數組:

[{"Nota":"ooo","Data":"14/01/2021"},{"Nota":"S02","Data":"15/01/2021"}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM