JSON..NET中的数组序列化



我试图创建一个用于序列化和反序列化数组的类。我所创建的类,似乎是为反序列化工作,但当我试图序列化数组,我有问题。我是一个相当新的c#开发人员,我确信我的代码中有一个重要的部分,我只是不确定是什么。

下面是我创建的类的副本:

namespace PinnacleCartFormAPI
{
    class clsGetCustomersResponse
    {
        public Customer Customer = new Customer();//{ get; set; }
    }
    public class Customer
    {
        public Int32 UserId;
        public string UserName;
        public CustBilling Billing = new CustBilling();
        public AddressBook[] AddressBook;// AddressBook = new AddressBook();
    }
    public class CustBilling
    {
        public string FullName, FirstName, LastName, Email, Company, Phone;
        public CustAddress Address = new CustAddress();
    }
    public class CustAddress
    {
        public string Name, Street1, Street2, City, State, Zip, Country;
    }
    public class AddressBook
    {
        public string Name, Street1, Street2, City, State, Zip, Country;
    }
}
如你所见,AddressBook类需要是一个Array。我认为我的问题与我没有正确地将AddressBook类初始化为数组有关。

下面是为类的不同元素添加值的调用代码的副本:

clsGetCustomersResponse GetCustomersResp = new clsGetCustomersResponse();
GetCustomersResp.Customer.UserId = 123456;
GetCustomersResp.Customer.UserName = "Username";
GetCustomersResp.Customer.Billing.FullName = "Full Name";
GetCustomersResp.Customer.Billing.FirstName = "First Name";
GetCustomersResp.Customer.Billing.LastName = "Last Name";    
GetCustomersResp.Customer.Billing.Email = "email@domain.com";
GetCustomersResp.Customer.Billing.Phone = "7778889999";
GetCustomersResp.Customer.Billing.Address.Name = "Address Name";
GetCustomersResp.Customer.Billing.Address.Street1 = "Address Street 1";
GetCustomersResp.Customer.Billing.Address.Street2 = "";
GetCustomersResp.Customer.Billing.Address.City = "Address City";
GetCustomersResp.Customer.Billing.Address.State = "Address State";
GetCustomersResp.Customer.Billing.Address.Zip = "Address Zip";
GetCustomersResp.Customer.Billing.Address.Country = "Address Country";

GetCustomersResp.Customer.AddressBook[0]。

GetCustomersResp.Customer.AddressBook[0].Street1 = "Address Street 1";
GetCustomersResp.Customer.AddressBook[0].Street2 = "";
GetCustomersResp.Customer.AddressBook[0].City = "Address City";
GetCustomersResp.Customer.AddressBook[0].State = "Address State";
GetCustomersResp.Customer.AddressBook[0].Zip = "Address Zip";
GetCustomersResp.Customer.AddressBook[0].Country = "Address Country";

一碰到加粗的行,我就收到以下错误:

"对象引用未设置为对象的实例"

再次,我认为这是我没有正确初始化代码的AddressBook部分的结果。但是,我不确定如何使用数组。

你能给我指点一下吗?

谢谢,

扎克

(只是为了清楚,这与JSON没有任何关系)

是的,在开始向数组中放入值之前,必须初始化数组。不幸的是,数组的大小是固定的——你不能在它们被创建后改变它的大小(例如,通过添加一个元素)。我建议使用List<AddressBook>而不是数组。然后你可以使用:

// This initialization could be in the type itself
GetCustomersResp.Customer.AddressBook = new List<AddressBook>();
AddressBook address = new AddressBook();
address.Name = "Address Name";
address.Street1 = "Address Street 1";
// etc
GetCustomersResp.Customer.AddressBook.Add(address);

我也想重命名AddressBook类型-它只是一个地址,而不是整个地址簿。Customer中的属性仍然可以称为AddressBook,因为地址簿地址的集合。

试试这个(10个地址簿):

GetCustomersResp.Customer.AddressBook = new AddressBook[10];

你需要实例化你的数组在你尝试分配元素之前

同意Jon的回答——我没有在上面花太多时间,但也许你会发现这组重构的类很有用:

namespace Pinnacle.Cart.Customers
{
    public class RetrieveResponse
    {
        public RetrieveResponse() { }
        public RetrieveResponse(Customer customer) { 
            Customer = customer;
        }
        public Customer Customer { get; set; }
    }
    public class Customer
    {
        public Customer() { 
            Billing = new BillingInfo(); 
            AddressBook = new List<AddressBookEntry>(); 
        }
        public Int UserId  { get; set; }
        public String UserName  { get; set; }
        public BillingInfo Billing  { get; set; }
        public List<AddressBookEntry> AddressBook { get; set; }
        public class BillingInfo
        {
            public BillingInfo() { Address = new Address(); }
            public String FullName { get; set; }
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Email { get; set; }
            public String Company { get; set; }
            public String Phone { get; set; }
            public Address Address { get; set; }
        }
    }
    public class Address
    {
        public String Street1 { get; set; }
        public String Street2 { get; set; }
        public String City { get; set; }
        public String State { get; set; }
        public String Zip { get; set; }
        public String Country { get; set; }
    }
    public class AddressBookEntry : Address
    {
        public string Name { get; set; }
    }
}

相关内容

  • 没有找到相关文章