using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
private void button1_Click(object sender, EventArgs e)
{
// 使用webapi引用组件Kingdee.BOS.WebApi.Client.dll
K3CloudApiClient client = new K3CloudApiClient("http://serverIP/k3cloud/");
var loginResult = client.ValidateLogin("databaseID", "userName", "Password", 2052);
var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
//登录结果类型等于1,代表登录成功
if (resultType == 1)
{
JObject jsonRoot = new JObject();
jsonRoot.Add("FieldKeys", "FBILLNO,FDATE,FMATERIALID,FMATERIALNAME,FREQQTY");//字段
JObject jsonModel = new JObject();
jsonModel.Add("FReqOrgIdList", "100784,1,100780,100781,100782,100783,100785");//申请组织
jsonModel.Add("FBeginBillNumber", txtReqNumBegin.Text);//开始单号
jsonModel.Add("FEndBillNumber", txtReqNumEnd.Text);//结束单号
jsonModel.Add("FOrderStartDate", "2001-01-01 00:00:00");//开始日期
jsonModel.Add("FOrderEndDate", "9999-03-10 00:00:00");//结束日期
jsonModel.Add("FDocumentStatus", "C");
jsonModel.Add("FLineStatus", "A");
jsonRoot.Add("Model", jsonModel);
string rowSet = client.GetSysReportData("PUR_PurReqExecuteRpt", jsonRoot.ToString());
DataTable dt = GetDataTableFromJsonString(rowSet);
dataGridView1.DataSource = dt;
}
}
public DataTable GetDataTableFromJsonString(string json)
{
var jsonLinq = JObject.Parse(json);
// Find the first array using Linq
var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
DataTable dt = ConvertToDataTable((JArray)srcArray);
return dt;
}
private DataTable ConvertToDataTable(JArray dataArr)
{
if(dataArr == null || dataArr.Count <= 0) return null;
DataTable result = new DataTable();
string[] columns = { "FBILLNO", "FDATE", "FMATERIALID", "FMATERIALNAME", "FREQQTY" };//对应的字段
foreach (string column in columns)
{
result.Columns.Add(column,Type.GetType("System.String"));
}
foreach (JToken item in dataArr)
{
DataRow dataRow = result.NewRow();
var brands = item.Values<string>().ToArray();
for (int i = 0; i < brands.Count(); i++)
{
string value = brands[i].ToString();
dataRow[columns[i]] = value;
}
result.Rows.Add(dataRow);
}
return result;
}