如何添加一个包含列表的新对象

how to add an new object with lists in it

我想创建一个新帐户,但如何使用其中的列表? 这是我的代码:

        account obj = new account
        {
            firstname = "..",
            surname = "..",
            job_title_id = ..,
            establishment_id = ..,
            email = "..",
            inherit_travel_allowance = ..,
            is_admin = ..,
            is_reporter = ..,
            is_exporter = ..,
            is_approver = ..,
            active = ..,
            free_1 = "..",
            free_2 = "..",
            free_3 = "..",
            creditor_nr = "..",
            approve_stages = new List<approve_stage>
            {
                policies = new List<ApprovePolicy>
                {
                    type = "..",
                    approver_id = ..
                }
            }
        };

        string response = account.Add(obj);

它说它不包含策略、类型和 approver_id 的定义。 这是 class:

public class account
{
    public int id { get; set; }
    public string firstname { get; set; }
    public string surname { get; set; }
    public bool active { get; set; }
    public int expense_count { get; set; }
    public string creditor_nr { get; set; }
    public string email { get; set; }
    public int customer_id { get; set; }
    public bool inherit_travel_allowance { get; set; }
    public int user_id { get; set; }
    public int job_title_id { get; set; }
    public int establishment_id { get; set; }
    public string free_1 { get; set; }
    public string free_2 { get; set; }
    public string free_3 { get; set; }
    public List<travel_allowance> travel_allowance { get; set; }
    public List<approve_stage> approve_stages { get; set; }
    public bool is_admin { get; set; }
    public bool is_reporter { get; set; }
    public bool is_exporter { get; set; }
    public bool is_approver { get; set; }
    public int[] disallowed_categories { get; set; }
    public int[] disallowed_payment_methods { get; set; }
    public int[] allowed_categories { get; set; }
    public int[] allowed_payment_methods { get; set; }
}
public class travel_allowance
{
    public int amount { get; set; }
    public string distance_unit { get; set; }
}
public class approve_stage
{
    public List<ApprovePolicy> policies { get; set; }
}
public class ApprovePolicy
{
    public string type { get; set; }
    public int? approver_id { get; set; }
}

我还没有找到任何关于如何解决这个问题的信息。 这个 "account object" obj 将被序列化为 json.

您混淆了 C# 和 JSON。

要填充 List<approve_stage>,您必须使用 ApprovePolicy

显式填充它
var obj = new account
{
  id = 100,
  // etc.
  approve_stages = new List<approve_stage> 
  {
    new approve_stage 
    {
      policies = new List<ApprovePolicy>
      {
        new ApprovePolicy
        {
          type = "Foo",
          approver_id = 100
        }
      }
    }
  }  
};