I'am trying to Read from a Deserilized Json Object. Currently I am using the following code to deserialize the JSON data:
var jsondecode = Newtonsoft.Json.JsonConvert.DeserializeObject(Request.Cookies.Get("wrkb").Value);
Content of my JSON object :
{{
"shoppingbasket": [
{
"price": 12,
"product": "Lachum",
"quantity": 2,
"total": 24
},
{
"price": 2,
"product": "Laici",
"quantity": 3,
"total": 12
},
{
"price": 12,
"product": "Lachum",
"quantity": 1,
"total": 12
}
]
}}
I want to convert this JSON string into a collection of .Net objects that I can work with.
For example, I would like to be able to product this type of output after I have deserialized the object.
Shoppingbasket:
1) Product = Lachum, Price = 12, Quantity = 3, Total = 36
2) Product = Laici, Price = 2, Quantity = 3, Total = 6
Total-Price of basket = 42
This was my solution but, pstrjds's solution elegant :
DataSet dataSet = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>(Request.Cookies.Get("wrkb").Value);
DataTable dataTable = dataSet.Tables["shoppingbasket"];
string print = "";
double basket_total = 0.0;
foreach (DataRow row in dataTable.Rows)
{
print = "Product= " + row["product"] + " Price= " + row["price"] + " Quantity= " + row["quantity"] + " Total= " + row["total"];
basket_total += Convert.ToInt32(row["total"]);
<div class="row">
<ul class="col-md-12">
<li><div class="col-md-12">@print</div></li>
</ul>
</div>
}
Now I have to fix duplicated Items