I'm trying to extract some values from data I received using "POST" method in c# webforms, sample data is:
{
"ProductStockDetailsID":8015425,
"LocationID":24213,
"ProductID":19284305,
"MinStock":null,
"MaxStock":null,
"OnOrder":0,
"Alerts":false,
"ProductStocks":[
{
"StockID":11839663,
"CurrentStock":68,
"CurrentVolume":0,
"CreatedDate":"2019-06-30T09:58:38.4",
"DeletedDate":null,
"CostPrice":0.00000,
"ProductStockDetailsID":8015425
}
]
}
I use Request.Form["ProductID"]
to extract the ProductID
successfully, But I'm not able to extract the CurrentStock
values because it looks to be nested from ProductStocks
.
Why not define the class for request and then just parse the request. TO do this you need to override ProcessRequest method like
public override void ProcessRequest(HttpContext context) { string json; using (var reader = new StreamReader(context.Request.InputStream)) { json = reader.ReadToEnd(); } var data = JsonConvert.DeserializeObject<RequestData>(json); base.ProcessRequest(context); ProcessRequest(data); } private void ProcessRequest(RequestData data) { // ... your code }
And the class to parse is:
public class RequestData { public int ProductStockDetailsID { get; set; } public int LocationID { get; set; } public int ProductID { get; set; } public object MinStock { get; set; } public object MaxStock { get; set; } public int OnOrder { get; set; } public bool Alerts { get; set; } public Productstock[] ProductStocks { get; set; } } public class Productstock { public int StockID { get; set; } public int CurrentStock { get; set; } public int CurrentVolume { get; set; } public DateTime CreatedDate { get; set; } public object DeletedDate { get; set; } public float CostPrice { get; set; } public int ProductStockDetailsID { get; set; } }
Now just use data.ProductStocks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.