C#通过接受通过Body传递过来的数据解析成 DataTable
前端传递过来的数据格式:
[{"成本类型":"外包服务","所属项目":"2024年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9680,"备注":"市场推广费:9680.00","内包外包":"外包"},
{"成本类型":"外包服务","所属项目":"2025年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9681,"备注":"市场推广费:9680.01","内包外包":"外包"},
{"成本类型":"外包服务","所属项目":"2026年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9682,"备注":"市场推广费:9680.02","内包外包":"外包"},
{"成本类型":"外包服务","所属项目":"2027年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9683,"备注":"市场推广费:9680.03","内包外包":"外包"},
{"成本类型":"外包服务","所属项目":"2028年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9684,"备注":"市场推广费:9680.04","内包外包":"外包"},
{"成本类型":"外包服务","所属项目":"2029年中国数字文娱大会","环节":"宣传","所属年":2024,"所属月":8,"项目组名称":"北京公司","金额":9685,"备注":"市场推广费:9680.05","内包外包":"外包"}]
在后端C#解析成DataTable:
string body = GetRequestBody().Result;//GetRequestBody().Result 自己封装的一个获取Body参数的方法 List<object> ssomProjectOutServicesCostImport = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(body); DataTable dt = new DataTable(); dt.Columns.Add("GroupName", Type.GetType("System.String")); dt.Columns.Add("SubjectTypeName", Type.GetType("System.String")); dt.Columns.Add("ProjectName", Type.GetType("System.String")); dt.Columns.Add("PayYear", Type.GetType("System.Int32")); dt.Columns.Add("PayMonth", Type.GetType("System.Int32")); dt.Columns.Add("LinkName", Type.GetType("System.String")); dt.Columns.Add("InnerOrOut", Type.GetType("System.String")); dt.Columns.Add("PayMoney", Type.GetType("System.Decimal")); dt.Columns.Add("PayRemark", Type.GetType("System.String")); foreach (var temp in ssomProjectOutServicesCostImport) { JToken jtTemp = (temp as Newtonsoft.Json.Linq.JToken); DataRow drtemp = dt.NewRow(); drtemp["GroupName"] = jtTemp["项目组名称"].ToString(); drtemp["SubjectTypeName"] = jtTemp["成本类型"].ToString(); drtemp["ProjectName"] = jtTemp["所属项目"].ToString(); drtemp["PayYear"] = jtTemp["所属年"].Value<int>(); drtemp["PayMonth"] = jtTemp["所属月"].Value<int>(); drtemp["LinkName"] = jtTemp["环节"].ToString(); drtemp["InnerOrOut"] = jtTemp["内包外包"].ToString(); drtemp["PayMoney"] = jtTemp["金额"].Value<decimal>(); ; drtemp["PayRemark"] = jtTemp["备注"].ToString(); dt.Rows.Add(drtemp); }