简体   繁体   English

从Request.form获取嵌套值

[英]Get Nested Value from Request.form

I'm trying to extract some values from data I received using "POST" method in c# webforms, sample data is: 我正在尝试从在C#Webforms中使用“ POST”方法接收的数据中提取一些值,示例数据为:

{  
   "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 . 我使用Request.Form["ProductID"]成功提取了ProductID ,但是我无法提取CurrentStock值,因为它看起来是嵌套在ProductStocks

Why not define the class for request and then just parse the request. 为什么不为请求定义类,然后仅解析请求。 TO do this you need to override ProcessRequest method like 为此,您需要覆盖ProcessRequest方法,例如

 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 现在只使用data.ProductStocks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM