语法问题将列表添加到对象

时间:2018-06-26 15:13:57

标签: c# json

我有从某些JSON数据创建的类。 JSON数据正确。

public class ARCustomers
{
    public ARCustomers()
    {
        CustomerOptionalFieldValues = new Dictionary<string, string>();
    }

    public string CustomerNumber { get; set; }
    public string ShortName { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string ContactName { get; set; }
    public string ContactsFax { get; set; }
    public string ContactsPhone { get; set; }
    public string Country { get; set; }
    public int CreditLimitCustomerCurrency { get; set; }
    public string CustomerName { get; set; }
    public string Email { get; set; }
    public string FaxNumber { get; set; }
    public string GroupCode { get; set; }
    public string OnHold { get; set; }
    public string PhoneNumber { get; set; }
    public string PrintStatements { get; set; }
    public string Salesperson1 { get; set; }
    public System.DateTime StartDate { get; set; }
    public string StateProvince { get; set; }
    public string Terms { get; set; }
    public string TerritoryCode { get; set; }
    public string ZipPostalCode { get; set; }
    public int NumberOfOptionalFields { get; set; }
    public string ProcessCommandCode { get; set; }
    public Dictionary<string, string> CustomerOptionalFieldValues { get; set; 
}

我正在尝试创建一个可以具有许多可选字段值的客户对象。该数字由输入数据确定。因此,它可能不是可选字段值,也可能是3、5或100。

因此,我创建了我的客户对象:

ARCustomers customer = new ARCustomers();

我填充了它:

customer.CustomerNumber = customers.Items[0].custNo;
customer.ShortName = customers.Items[0].custNo;
customer.CustomerName = customers.Items[0].custAddresses[0].name;
... more values filled in ...

如果要添加一些可选字段,如何创建可选字段对象,并将其添加到客户对象?

想法是将其转换为JSON请求并传递给网络api。

要说明我需要获取的JSON,这是我想要的结构:

{
  "customerNumber": null,
  "shortName": null,
  "addressLine1": null,
  "addressLine2": null,
  "city": null,
  "contactName": null,
  "contactsFax": null,
  "contactsPhone": null,
  "country": null,
  "creditLimitCustomerCurrency": 0,
  "customerName": null,
  "email": null,
  "faxNumber": null,
  "groupCode": null,
  "onHold": null,
  "phoneNumber": null,
  "printStatements": null,
  "salesperson1": null,
  "startDate": "",
  "stateProvince": null,
  "terms": null,
  "territoryCode": null,
  "zipPostalCode": null,
  "numberOfOptionalFields": 0,
  "processCommandCode": null,
  "customerOptionalFieldValues": [
  {
     "customerNumber": null,
     "optionalField": null,
     "value": null
  },
  {
     "customerNumber": null,
     "optionalField": null,
     "value": null
  }]
}

2 个答案:

答案 0 :(得分:0)

我认为最好用一个以属性名称作为关键字并保存该属性值的字典来代替CustomerOptionalFieldValue

public class ARCustomers
{
    public ARCustomers()
    {
         CustomerOptionalFieldValues = new Dinctionary<string, object>();
    }

    public string CustomerNumber { get; set; }
    public string ShortName { get; set; }
    .....
    public Dictionary<string, object> CustomerOptionalFieldValues { get; set; }

}

然后您可以在此字典中输入可选值:

ARCustomers arCustomers = new ARCustomers();
arCustomers.CustomerOptionalFieldValues.Add("customerNumber", null);
arCustomers.CustomerOptionalFieldValues.Add("optionalField", 1);
....

然后可以更轻松地使用字典进行后续步骤。

编辑1:

在这种情况下,如果可选字段中有多个json数据,则应使用List<Dictionary<string, object>>而不是Dictionary<string, object>,如下面的小提琴所示:

单击here查看示例:

答案 1 :(得分:0)

班级代码变成这样:

public class COFXCompanyNumber
{
    public string CustomerNumber { get; set; }
    public string OptionalField { get; set; }
    public string Value { get; set; }
}

public class ARCustomers
{
    public string CustomerNumber { get; set; }
    public string ShortName { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string ContactName { get; set; }
    public string ContactsFax { get; set; }
    public string ContactsPhone { get; set; }
    public string Country { get; set; }
    public int CreditLimitCustomerCurrency { get; set; }
    public string CustomerName { get; set; }
    public string Email { get; set; }
    public string FaxNumber { get; set; }
    public string GroupCode { get; set; }
    public string OnHold { get; set; }
    public string PhoneNumber { get; set; }
    public string PrintStatements { get; set; }
    public string Salesperson1 { get; set; }
    public System.DateTime StartDate { get; set; }
    public string StateProvince { get; set; }
    public string Terms { get; set; }
    public string TerritoryCode { get; set; }
    public string ZipPostalCode { get; set; }
    public int NumberOfOptionalFields { get; set; }
    public string ProcessCommandCode { get; set; }
    public IList<COFXCompanyNumber> CustomerOptionalFieldValues { get; set; }
}

以这种方式添加可选字段会将它们放入目标系统似乎喜欢的集合中。这为我解决了这个问题。