将列表中的两个不同实例序列化为单个json字符串

use*_*822 4 c# serialization json

我有两种类:

public class HolidayClass
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public bool Active { get; set; }

    public HolidayClass(int ID, string Name, DateTime StartDate, DateTime EndDate, bool Active)
    {
        this.ID = ID;
        this.Name = Name;
        this.StartDate = StartDate;
        this.EndDate = EndDate;
        this.Active = Active;
    }

    public HolidayClass()
    {
    }
}

public class ProjectClass
{
    public int ID { get; set; }
    public string NetsisID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public bool Active { get; set; }

    public ProjectClass(int ID, string NetsisID, string Name, string Address, bool Active)
    {
        this.ID = ID;
        this.NetsisID = NetsisID;
        this.Name = Name;
        this.Address = Address;
        this.Active = Active;
    }
    public ProjectClass()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有两个列表项.

List<ProjectClass> pc;
List<HolidayClass> hc;
Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令序列化单个列表:

myJson = new JavaScriptSerializer().Serialize(pc).ToString();
Run Code Online (Sandbox Code Playgroud)

要么

myJson = new JavaScriptSerializer().Serialize(hc).ToString();
Run Code Online (Sandbox Code Playgroud)

我想在一个json字符串中序列化这两个列表.我怎样才能做到这一点?

Ufu*_*arı 5

最明智的做法是为序列化创建一个新类型或使用匿名类型:

var objects = new { HolidayClasses = hc, ProjectClasses = pc };
string result = new JavaScriptSerializer().Serialize(objects);
Run Code Online (Sandbox Code Playgroud)