简体   繁体   English

使用子对象反序列化 Json 字符串

[英]Deserialize Json string with child objects

Got the following structure given:得到以下结构:

public class TaskList
{
    public string Name { get; set; }
    public List<ToDoTask> ToDoTasks { get; set; }
}

public class ToDoTask
{
    public string Name { get; set; }
    public string Note { get; set; }
    public DateTime LastEdit { get; set; }
    public bool Finished { get; set; }
}

I'm using System.Text.Json in .NET 5.0 to serialize a List successfully into a json-file:我在 .NET 5.0 中使用System.Text.Json将列表成功序列化为 json 文件:

JsonSerializerOptions serializeOptions = new() { WriteIndented = true };
string json = JsonSerializer.Serialize(taskLists, serializeOptions);

the result looks fine:结果看起来不错:

{
  "TaskLists": [
    {
      "Name": "List1",
      "ToDoTasks": [
        {
          "Name": "Task1",
          "Note": "",
          "LastEdit": "2022-04-19T13:05:10.0415588+02:00",
          "Finished": false
        },
        {
          "Name": "Task2",
          "Note": "",
          "LastEdit": "2022-04-19T13:05:13.9269202+02:00",
          "Finished": false
        }
      ]
    },
    {
      "Name": "List2",
      "ToDoTasks": [
        {
          "Name": "Task3",
          "Note": "",
          "LastEdit": "2022-04-19T13:05:18.3989081+02:00",
          "Finished": false
        },
        {
          "Name": "Task4",
          "Note": "",
          "LastEdit": "2022-04-19T13:05:23.0949034+02:00",
          "Finished": false
        }
      ]
    }
  ]
}

When I deserialize this json-file, I only got the TaskLists but the ToDoTasks, are empty.当我反序列化这个 json 文件时,我只得到了 TaskLists,但 ToDoTasks 是空的。

List<TaskList> taskLists = JsonSerializer.Deserialize<List<TaskList>>(json);

What do I have to do, get also the ToDoTask-Childs included into the deserialized objects?我必须做什么,还要将 ToDoTask-Childs 包含在反序列化的对象中?

Whenever you cannot figure out your model class, you can use Visual Studio's Edit - Paste Special - Paste JSON as Class to check out.每当你搞不清楚你的 model class 时,你可以使用 Visual Studio 的Edit - Paste Special - Paste JSON as Class来查看。

Your model classes should be like this:你的 model 类应该是这样的:

public class Rootobject
{
    public Tasklist[] TaskLists { get; set; }
}

public class Tasklist
{
    public string Name { get; set; }
    public Todotask[] ToDoTasks { get; set; }
}

public class Todotask
{
    public string Name { get; set; }
    public string Note { get; set; }
    public DateTime LastEdit { get; set; }
    public bool Finished { get; set; }
}

And you can Deserialize it:你可以反序列化它:

static void Main(string[] args)
{
    var query = JsonSerializer.Deserialize<Rootobject>(File.ReadAllText("data.json"));
}

Your json has a root object containing the task list, so List<TaskList> does not represent it correctly.您的 json 有一个包含任务列表的根 object,因此List<TaskList>不能正确表示它。 Try:尝试:

public class Root
{
    public List<TaskList> TaskLists { get; set; }
}

var root = JsonSerializer.Deserialize<Root>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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