我正在尝试为我的项目创建带有RDLC
阵列的JSON
报告。此JSON
数组来自API。我已经使用数据库制作了RDLC
报告,因为Dataset
直接访问了SQL database
,但是如何处理来自JSON
数组的API。
我正在尝试在未类型化的数据表上获取JSON
数组,然后将其合并到类型化的数据表中。然后应该显示在dataset.xsd
上,但不会显示。
public partial class WebForm2 : System.Web.UI.Page
{
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
protected void Page_Load(object sender, EventArgs e)
{
test();
}
public void test()
{
WebClient client = new WebClient();
string strJSON = client.DownloadString("Json.txt");
string chk = strJSON.Replace("\\", "");
chk = chk.Substring(1, chk.Length - 2);
DataTable dtUsingMethod = GetJSONToDataTableUsingNewtonSoftDll(chk);
DataSet1.test_tableDataTable tbl = new DataSet1.test_tableDataTable();
tbl.Merge(dtUsingMethod);
}
public static DataTable GetJSONToDataTableUsingNewtonSoftDll(string JSONData)
{
DataTable dt = (DataTable)JsonConvert.DeserializeObject(JSONData, (typeof(DataTable)));
return dt;
}
}
JSON格式如下:
"[{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"6\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"270.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"410.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"},{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"1\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"260.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"395.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"},{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"2\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"260.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"395.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"}]"
预期结果是我想使用JSON数组生成报告。
答案 0 :(得分:0)
好的,我找到了一个对我有用的答案。我希望将其发布在这里,这对其他人也有帮助。 这里的问题是我们需要RDLC报告的数据源。由于我们无法使用Sql数据库,因此必须创建自己的自定义数据源。
步骤1.添加类文件。
第2步。类文件中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using System.Web.Services;
using System.Net;
namespace Batch_report
{
public class batch_data
{
public string plant_sl_no { get; set; }
public string batch_no { get; set; }
public string batch_no_sl { get; set; }
public string batch_index { get; set; }
public string batch_date { get; set; }
public string recp_id { get; set; }
public string recp_name { get; set; }
public string pdt_qty { get; set; }
public string truck_id { get; set; }
public string cust_id { get; set; }
public string load_sent_qty { get; set; }
public string site { get; set; }
}
public class Custom_batch
{
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static List<batch_data> GetEmployeeName()
{
//List<string> abc = new List<string>();
//string[] arr;
WebClient client = new WebClient();
string info = client.DownloadString("http://json.txt");
string chk = info.Replace("\\", "");
chk = chk.Substring(1, chk.Length - 2);
List<batch_data> obj = new List<batch_data>();
obj = JsonConvert.DeserializeObject<List<batch_data>>(chk);
return obj;
}
}
}
步骤3.构建您的类文件,您将获得batch_report.dll文件。将此.dll添加到项目的参考文件夹中。
步骤4.从工具箱添加报告或报告向导。添加后,将打开一个窗口,询问数据集。命名您的数据集,然后在数据源中从下拉列表中选择batch_report。在“可用数据集”中,选择要从中获取数据的方法,在我的情况下,该方法来自Custom_batch类,方法是-GetEmployeeName()。
步骤5。单击“下一步”直到完成,然后您还可以设计报告。