User987
User987

Reputation: 3823

Replicating PHP's array of arrays and inside it array of array in c#

I have written a piece of code in PHP which basically looks like this:

$order = array( 
'0' => array( 
'user_order_sn' => '123', 
'country' => 'some', 
'firstname' => '123', 
'lastname' => '123', 
'addressline1' => 'AFDAFAF', 
'addressline2' => '', 
'shipping_method' => '123', 
'tel' => '551245', 
'state' => '4444', 
'city' => '55r', 
'zip' => '1004451', 
'order_remark' => 'test', 
'order_platforms' => 3, 
'original_order_id' => '7126216', 
'original_account' => '51251251', 
'original_order_amount' => 2.57, 
'goods_info' => array( 0 => array( 'goods_sn' => '6544321', 'goods_number' => 4
)
),
),
);

So as you can see order variable is array of arrays which contains a variable goods_info which also array of array inside it.

I would like to replicate this in c#. I suspect I need to use jagged arrays here, but I'm not 100% sure how to do that. I created a class for starters which contain all the info above:

  public class CreateOrderDataRequest
    {
        public string user_order_sn { get; set; }
        public string country { get; set; }

        public string firstname { get; set; }
        public string lastname { get; set; }

        public string addressline1 { get; set; }

        public string addressline2 { get; set; }
        public string shipping_method { get; set; }
        public string tel { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string zip { get; set; }

        public string order_remark { get; set; }
        public string order_platforms { get; set; }
        public string original_order_id { get; set; }

        public string original_account { get; set; }
        public string original_order_amount { get; set; }
    }

Can someone help me out to finish this ? :)

P.S. I haven't finished the goods_info part because I'm not sure how to do that ...

Upvotes: 1

Views: 31

Answers (1)

ninja coder
ninja coder

Reputation: 1117

Create a new class called GoodsInfo. Then add a List<GoodsInfo> to your CreateOrderDataRequest.

public class Order
{
    public class Order()
    {
        Requests = new List<OrderDataRequest>();
    }

    public List<OrderDataRequest> Requests { get; set; }
    //OR
    public OrderDataRequest[] Requests { get; set; }
}

public class OrderDataRequest
{
    public OrderDataRequest()
    {
        GoodsInfos = new List<GoodsInfo>();
    }

    public string user_order_sn { get; set; }
    .
    .
    .
    public List<GoodsInfo> GoodsInfos {get; set;}
    //OR
    public GoodsInfo[] GoodsInfo { get; set; }
}

public class GoodsInfo
{
    public string goods_sn { get; set; }
    public string good_number { get; set; }
}

Edit: I updated the code to add the full class structure. Additionally, I added initialization of the Lists in a constructor which you may need to do depending on how you are creating your objects.

Upvotes: 1

Related Questions