What I'm trying to do is to get some data from my database (customers order with all items he ordered) convert it to JSON that contains a list of said items so I can send it with a POST request. I want to create a JSON object which would contain details of customers order, and a list of products contained in that order. This is how I want my JSON to look like:
{
"account_code": "00000",
"advance_payment": false,
"date": [today's date],
"order_items":
[
{
"item_id": 0,
"product_Price ": "105.00",
"order_Item_Rebate: "",
"product_code": "000001",
"quantity": 1
},
{
"item_id": 1,
"product_Price ": "55.00",
"price": "",
"product_code": "000002",
"quantity": 5
},
{
"item_id": 2,
"product_Price ": "15.00",
"price": "",
"product_code": "000003",
"quantity": 3
}
]
}
But what I'm getting is this:
{
"account_code": "00000",
"advance_payment": false,
"date": [today's date],
"order_items":
[
{
"item_id": 0,
"roduct_Price ": "105.00",
"order_Item_Rebate: "",
"product_code": "000001",
"quantity": 1
}
]
},
{
"account_code": "00000",
"advance_payment": false,
"date": [today's date],
"order_items":
[
{
"item_id": 1,
"roduct_Price ": "55.00",
"price": "",
"product_code": "000002",
"quantity": 5
}
]
},
{
"account_code": "00000",
"advance_payment": false,
"date": [today's date],
"order_items":
[
{
"item_id": 2,
"product_Price ": "15.00",
"price": "",
"product_code": "000003",
"quantity": 3
}
]
}
Instead of one JSON object with a list of items, I get multiple JSON object for the same customer(account_code).
I'm getting this data from database using ADO.NET and I read this data with SqlDataReader. Inside SqlDataReader I created a new object and filled it with details about my order. It all works, but problem is order_items which should be a list of all items from the order. I want one order with multiple items in it, not a different order for every item. Using code below I am able to get data from my database, but I don't know how to group order_items with the order they should be in.
List<OrderModel> orderList = new List<OrderModel>();
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (rdr.Read())
{
OrderModel order = new OrderModel()
{
Account_code = rdr[0].ToString(),
Advance_payment = false,
Date = DateTime.Now.Date,
Order_Items = new List<Order_items>()
{
//Here is where I should do something instead of creating new
//Order_items, but I don't know what
new Order_items()
{
Order_Item_Rebate = rdr[1].ToString(),
Product_Price = rdr[2].ToString(),
Product_Code = rdr[3].ToString(),
Quantity = rdr[4].ToString()
}
}
};
orderList.Add(order);
}
var jsonString = JsonConvert.SerializeObject(orderList);
return jsonString;
}
My OrderModel class looks like this:
public class OrderModel
{
public string Account_code { get; set; }
public bool Advance_payment { get; set; }
public DateTime Date { get; set; }
public List<Order_items> Order_Items { get; set; }
}
public class Order_items
{
public string Order_Item_Rebate { get; set; }
public string Product_Price { get; set; }
public string Product_Code { get; set; }
public string Quantity { get; set; }
}
And table from my database looks like this:
Account_code | Order_Item_Rebate | Product_Price | Product_Code | Quantity |
---|---|---|---|---|
00000 | 10 | 105 | 000001 | 1 |
00000 | 5 | 55 | 000002 | 5 |
00000 | 15 | 15 | 000002 | 3 |
I have seen similar questions and people were advising to use an array of dictionaries, but I'm not sure if I understand how would they help. I'm new with this, so I'm sorry if question is not really smart.
OrderModel
for eachOrder_items
.