2011-10-26 21 views
0

配列のシリアライズとデシリアライズのためのクラスを作成しようとしています。私が作成したクラスは、デシリアライズのために働いているようですが、配列をシリアル化しようとすると問題が発生します。私はかなり新しいC#開発者であり、コードから重要な部分を除外していると確信しています。以下はC#.NETのJSON.NET配列のシリアル化

私が作成したクラスのコピーです:

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クラスを正しく開始していないという事実に関連していると思います。

以下

は、クラスの異なる要素に値を追加し、呼び出し元のコードのコピーである:私は太字の行を打つとすぐに

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 protected]"; 
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].Name = "Address Name";

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部分を正しく初期化していないためです。しかし、私は配列でそれを行う方法が特定されていません。

私にこれに関するいくつかの方向を教えてください。

おかげで、

ザック

答えて

1

試してみてください。この(10アドレス帳用):あなたが要素を割り当てようとする前に、あなたはあなたの配列をインスタンス化する必要が

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

それ

2

( JSONとはまったく関係ありません。

はい、値を入力する前に配列を初期化する必要があります。残念ながら、配列は固定サイズです。作成後にサイズを変更することはできません(要素を追加するなど)。私は配列の代わりにList<AddressBook>を使用することをお勧めします。次に、あなたが使用することができます。私も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); 

- それはただ一つのアドレスではなく、全体のアドレス帳です。 Customer内のプロパティは、アドレスブックのアドレスコレクションであるため、依然としてAddressBookと呼びます。 Jonの答えに同意した

+0

ジョン、 を私はエラーが発生しました: "暗黙のうちにタイプ 'System.Collections.Generic.List 'をPinnacleCartFormAPI.AddressBookに変換できません。ここで間違っていることを説明していただけますか?私のクラスのAddressBookセクションを変更する必要がありますか? – WebTech

+0

@WebTech: 'Customer'クラスの' AddressBook' *プロパティ*を 'List '(あるいはおそらく 'IList ')に変更する必要があります。 –

+0

ありがとうございました。あなたが提供したものを使用して、私が期待するようにすべてが機能します。 – WebTech

0

- 私はそれに時間のトンを費やすことはありませんでしたが、多分あなたは便利なクラスのこのリファクタリングセット見つけることができます:私はあなたが持っているものを追加すると

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; } 
    } 
} 
+0

ありがとうございました。私はこれらの変更を実装し、物事がどのように進むかを見ていきます。 – WebTech

関連する問題