C# 向对象添加列表时出现的语法问题

C# 向对象添加列表时出现的语法问题,c#,json,C#,Json,我有一个从一些JSON数据创建的类。JSON数据是正确的 public class ARCustomers { public ARCustomers() { CustomerOptionalFieldValues = new Dictionary<string, string>(); } public string CustomerNumber { get; set; } public string ShortName { g

我有一个从一些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; 
}
我填充它:

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请求并传递给web 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
  }]
}

我认为最好将这个
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; }

}
接下来的步骤更容易使用字典

编辑1:

在这种情况下,如果可选字段中有多个json数据,则应使用
List
而不是
Dictionary
,如下所示:


单击查看示例:

类代码如下:

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; }
}
公共类COFXCompanyNumber
{
公共字符串CustomerNumber{get;set;}
公共字符串选项字段{get;set;}
公共字符串值{get;set;}
}
公共类客户
{
公共字符串CustomerNumber{get;set;}
公共字符串短名称{get;set;}
公共字符串AddressLine1{get;set;}
公共字符串AddressLine2{get;set;}
公共字符串City{get;set;}
公共字符串ContactName{get;set;}
公共字符串ContactsFax{get;set;}
公共字符串ContactsPhone{get;set;}
公共字符串国家{get;set;}
公共int CreditLimitCustomerCurrency{get;set;}
公共字符串CustomerName{get;set;}
公共字符串电子邮件{get;set;}
公共字符串传真号码{get;set;}
公共字符串组代码{get;set;}
保留的公共字符串{get;set;}
公共字符串PhoneNumber{get;set;}
公共字符串打印语句{get;set;}
公共字符串salerson1{get;set;}
public System.DateTime开始日期{get;set;}
公共字符串StateProvince{get;set;}
公共字符串项{get;set;}
公共字符串TerritoryCode{get;set;}
公共字符串ZipPostalCode{get;set;}
公共int NumberOfOptionalFields{get;set;}
公共字符串ProcessCommandCode{get;set;}
公共IList CustomerOptionalFieldValues{get;set;}
}

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

hi@snert,你是说有一个对象可以动态地保存可选字段吗?例如,如果json包含3个可选字段,那么对象也应该包含3个字段,以此类推?谢谢,我会尝试一下:)哦,这简直是太棒了。当我生成customer对象时,我推送数据。我将它发送到我的函数,以推入目标系统,它使用以下命令对其进行序列化:JsonConvert.SerializeObject(有效负载)。在序列化的JSON字符串中,它遗漏了[and]来包围CustomerOptionalFieldValue。JSON在有和没有这些选项的情况下都是有效的,但出于某种原因,我使用的目标系统需要嵌入选项周围的[and]。有什么想法吗?是的,当然它可以由json序列化程序控制,但是你能给我一个使用[and]语法的例子,这样我就可以详细阐述更多内容吗?你的意思是像在上面的json中那样在自定义数据字段周围添加这些[]吗?是的,customerOptionalFields需要在块周围有[and]。因此,如果有多个可选字段,则块周围会有一组方括号。我已经更新了上面的JSON,以显示多个可选字段以及如何使用方括号
ARCustomers arCustomers = new ARCustomers();
arCustomers.CustomerOptionalFieldValues.Add("customerNumber", null);
arCustomers.CustomerOptionalFieldValues.Add("optionalField", 1);
....
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; }
}