【问题标题】:how to add an new object with lists in it如何添加一个包含列表的新对象
【发布时间】:2015-02-19 13:38:18
【问题描述】:

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

        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 的定义。 这是课程:

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。

【问题讨论】:

  • 您忘记创建新的approve_stage 和策略来填充您的列表
  • @Sayse 我不明白你的回答,请给我解释一下
  • 查看 GeirGrusom 的回答,它创建了一个阶段和一个策略... 也是题外话,但 C# 倾向于使用驼峰式套管
  • 感谢您的快速反应,它现在可以工作了...下次我将使用骆驼壳:)

标签: c# json object serialization


【解决方案1】:

您混淆了 C# 和 JSON。

要填充 List&lt;approve_stage&gt;,您必须使用 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
        }
      }
    }
  }  
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多