我想创建一个新帐户,但如何在其中使用列表? 这是我的代码:
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);
它表示它不包含policy,type和approver_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; }
}
我没有找到有关如何解决此问题的任何信息。 这个“帐户对象”obj将被序列化为json。
答案 0 :(得分:2)
你混淆了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
}
}
}
}
};