0

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:

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.

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

What do I have to do, get also the ToDoTask-Childs included into the deserialized objects?

2
  • Please provide your .NET version Commented Apr 19, 2022 at 11:30
  • It's .NET 5.0, I've added it to the post Commented Apr 19, 2022 at 11:47

2 Answers 2

1

Whenever you cannot figure out your model class, you can use Visual Studio's Edit - Paste Special - Paste JSON as Class to check out.

Your model classes should be like this:

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"));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your json has a root object containing the task list, so List<TaskList> does not represent it correctly. Try:

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

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

2 Comments

Exactly the same result. That alos was my first thought, but nothing changed
@Seraphim works for me - see the repro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.