小编Luc*_*Luc的帖子

使用 System.Text.Json 将 JSON 反序列化为对象

我尝试使用System.Text.Json.Serialization命名空间将 JSON 文件中的文本反序列化为名为 Note 的对象,然后访问其属性。稍后的目的是读入多个 Note 对象,然后存储在列表中。

除了 DOTNET 文档https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-之外,似乎没有很多关于使用此命名空间的示例

这是我根据给出的例子进行的尝试。这会引发如下所示的错误,如果您知道我做错了什么,请告诉我,谢谢。

class Note
{
    public DateTime currentDate { get; set; }
    public string summary { get; set; }
    public Note(DateTime _date, string _sum)
    {
        currentDate = _date;
        summary = _sum;
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Write json data
        string path = @"D:\Documents\Projects\Visual Projects\Notes Data\ThingsDone.json";

        DateTime date = DateTime.Now;
        string givenNote = "summary text";

        Note completeNote = new Note(date, givenNote);

        string serialString …
Run Code Online (Sandbox Code Playgroud)

c# json json-deserialization system.text.json

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

json ×1

json-deserialization ×1

system.text.json ×1