Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何添加包含列表的新对象_C#_Json_Object_Serialization - Fatal编程技术网

C# 如何添加包含列表的新对象

C# 如何添加包含列表的新对象,c#,json,object,serialization,C#,Json,Object,Serialization,我想创建一个新帐户,但如何在其中使用列表? 这是我的代码: account obj = new account { firstname = "..", surname = "..", job_title_id = .., establishment_id = .., email = "..", inherit_travel

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

        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);
accountobj=新账户
{
firstname=“…”,
姓“…”,
工作职位id=。。,
机构识别号=。。,
电子邮件=“…”,
继承差旅津贴=。。,
是_admin=。。,
是记者吗。。,
是_exporter=。。,
是否批准人=。。,
活动=。。,
自由_1=“…”,
自由_2=“…”,
自由_3=“…”,
债权人_nr=“…”,
批准阶段=新列表
{
策略=新列表
{
type=“…”,
批准人\u id=。。
}
}
};
字符串响应=account.Add(obj);
它说它没有包含策略、类型和批准者id的定义。 这是一节课:

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; }
}
公共类帐户
{
公共int id{get;set;}
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
公共bool活动{get;set;}
公共整数费用_计数{get;set;}
公共字符串\u nr{get;set;}
公共字符串电子邮件{get;set;}
public int customer_id{get;set;}
公共学校继承差旅津贴{get;set;}
公共int用户_id{get;set;}
public int job_title_id{get;set;}
公共int建立_id{get;set;}
公共字符串空闲_1{get;set;}
公共字符串自由_2{get;set;}
公共字符串自由_3{get;set;}
公共列表旅行津贴{get;set;}
公共列表批准{get;set;}
公共bool是_admin{get;set;}
公共布尔是{get;set;}
公共布尔是{get;set;}
公共bool是_批准者{get;set;}
public int[]不允许的_类别{get;set;}
public int[]不允许的支付方法{get;set;}
公共int[]允许的_类别{get;set;}
public int[]允许的支付方法{get;set;}
}
公务舱旅行津贴
{
公共整数金额{get;set;}
公共字符串距离_单位{get;set;}
}
公开课阶段
{
公共列表策略{get;set;}
}
公共类批准策略
{
公共字符串类型{get;set;}
公共int?批准者_id{get;set;}
}
我没有找到任何关于如何解决这个问题的信息。 这个“帐户对象”对象将被序列化为json。

你把C#和json搞混了

要填充
列表
,必须使用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
        }
      }
    }
  }  
};
var obj=新账户
{
id=100,
//等等。
批准阶段=新列表
{
新阶段
{
策略=新列表
{
新批准政策
{
type=“Foo”,
批准人\u id=100
}
}
}
}  
};

您忘记创建新的
批准阶段和策略来填充列表with@Sayse我不明白你的回答,请向meSee GeirGrusom的回答解释一下,它创造了一个阶段和一个政策。。。话题也离题了,但C#倾向于使用驼壳手杖来快速反应,现在它已经开始工作了。。。下次我将使用骆驼壳:)