c# - 调用 tojson() 后创建 3 个 List<column>

标签 c# json file

我们创建了一个新类,它的构造函数设置一个电子邮件字符串和一个由 3 个对象组成的列表,默认情况下每列包含一个空 List<task> .

我们尝试使用函数 toJson() 将板保存到 .json 文件中但是.json文件仅包含电子邮件字符串,我们需要 .json文件包含电子邮件和 List<column>也是如此。

class Board : DalObject<Board>
{
    private readonly log4net.ILog log = log4net.LogManager.GetLogger(
          System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private string email;
    private List<Column> columns;
    public Board() { }
    public Board(string email, List<Column> columns)
    {
        this.Email = email;
        this.Columns = columns;
    }
    public string Email { get => email; set => email = value; }
    internal List<Column> Columns { get => columns; set => columns = value; }


    public string ToJson()
    {
        var json = new JsonSerializerOptions
        {
            WriteIndented = true
        };
        log.Info("Board of user " + this.Email + " saved");
        return JsonSerializer.Serialize(this, json);
    }
    public void Save()
    {
        base.controller.WriteBoard(this.Email, ToJson());
    }
}

class Column : DalObject<Column>
{
    private int columnOrdinal;
    private string columnName;
    private int limit;
    private List<Task> tasks;
    public Column() { }
    public Column(int columnOrdinal, string columnName, int limit, List<Task> tasks)
    {
        this.ColumnOrdinal = columnOrdinal;
        this.ColumnName = columnName;
        this.Limit = limit;
        this.Tasks = tasks;
    }

    public int ColumnOrdinal { get => columnOrdinal; set => columnOrdinal = value; }
    public string ColumnName { get => columnName; set => columnName = value; }
    public int Limit { get => limit; set => limit = value; }
    internal List<Task> Tasks { get => tasks; set => tasks = value; }
}

class Task : DalObject<Task>
{
    private int taskId;
    private string title;
    private string description;
    private DateTime creationDate;
    private DateTime dueDate;

    public Task() { }

    public Task(int taskId, string title, string description, DateTime dueDate, DateTime creationDate)
    {
        this.TaskId = taskId;
        this.Title = title;
        this.Description = description;
        this.CreationDate = creationDate;
        this.DueDate = dueDate;
    }
    public int TaskId { get => taskId; set => taskId = value; }
    public string Title { get => title; set => title = value; }
    public string Description { get => description; set => description = value; }
    public DateTime CreationDate { get => creationDate; set => creationDate = value; }
    public DateTime DueDate { get => dueDate; set => dueDate = value; }
}

public void WriteBoard(string fileName, string content)
{
    File.WriteAllText(BaseBoard + fileName + ".json", content);
    log.Info("The new data saved");
}

最佳答案

您可以使用JSON Serializer将您的类(包含 List 或其他数据)序列化为 JSON 并将其保存到文件:

var content= JsonConvert.SerializeObject(obj, Formatting.Indented);
File.WriteAllText(BaseBoard + fileName + ".json", content);

关于c# - 调用 tojson() 后创建 3 个 List<column>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294415/

相关文章:

Javascript 问题已解决,不明白问题出在哪里

ruby - 使用 Faraday Ruby gem 下载图像并写入磁盘

c++ - 无法读取 Unicode 字符 - C++

c# - Linq 到 SQL : Get Top 5 Rated products

c# - UWP 网络安装 - “Error in parsing the app package.”

c# - 使用 msbuild 14/VS 2015 干净地构建的简单代码,但无法在 msbuild 15/VS 2017 中编译

java - Json 忽略类名而不是属性

c# - 带有 where 子句的 LINQ 语句使执行变慢

javascript - 如何在 Vue.js 中正确延迟加载 json 文件以减少包大小?

sql - Azure 逻辑应用程序 - 解析 JSON 架构失败 "Required properties are missing from object"