繁体   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