问题描述
usingNewtonsoft.Json.Linq;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Net.Http.Headers;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingUtilTool;namespace数据获取新{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatestringGetAccessToken(){stringAppKey="255756";stringAppSecret="d9NCo9GNiSHyVyRwQM8Fa7oL5iVQTYgn";HttpClient_httpClient=newHttpClient();_httpClient.BaseAddress=newUri("http://auth.sunton.cn");_httpClient.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Basic",Convert.ToBase64String(Encoding.ASCII.GetBytes(AppKey+":"+AppSecret)));varparameters=newDictionary<string,string>();parameters.Add("grant_type","client_credentials");varresponse=_httpClient.PostAsync("/token",newFormUrlEncodedContent(parameters));varresponseValue=response.Result.Content.ReadAsStringAsync().Result;returnresponseValue;}//接口调用publicstringTest(stringAPI){vartoken=GetAccessToken();token=JObject.Parse(token)["access_token"].Value<string>();HttpClient_httpClient=newHttpClient();_httpClient.BaseAddress=newUri("http://open.sunton.cn");_httpClient.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Bearer",token);stringresult=_httpClient.GetAsync(API).Result.Content.ReadAsStringAsync().Result;returnresult;}DataTabletable=null;privatevoidbutton1_Click(objectsender,EventArgse){if(!backgroundWorker1.IsBusy){button1.Enabled=false;button2.Enabled=false;backgroundWorker1.RunWorkerAsync();}}privatevoidbutton2_Click(objectsender,EventArgse){if(saveFileDialog1.ShowDialog()==DialogResult.OK){if(!backgroundWorker2.IsBusy){button1.Enabled=false;button2.Enabled=false;backgroundWorker2.RunWorkerAsync();}}}privatevoidbackgroundWorker1_DoWork(objectsender,DoWorkEventArgse){try{table=newDataTable();DataColumndtc=newDataColumn("NoteID",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("NoteCode",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("AddTime",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("OperaterName",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Type",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Description",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("AuditStatus",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("AuditorName",typeof(string));table.Columns.Add(dtc);//*********************************************************dtc=newDataColumn("RecordAddTime",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("ResellerID",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("ResellerName",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Amount",typeof(string));table.Columns.Add(dtc);//*********************************************************dtc=newDataColumn("StockInDetail",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("ProductID",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("ProductName",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("OuterCode",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("SKUID",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("SKUOuterCode",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Quantity",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Price",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("TotalPrice",typeof(string));table.Columns.Add(dtc);dtc=newDataColumn("Status",typeof(string));table.Columns.Add(dtc);//stringApi=textBox1.Text;stringjson=Test(Api);Regexre=newRegex(""Total":([^,]*),",RegexOptions.Multiline|RegexOptions.IgnoreCase);label1.Text="总条数:"+re.Match(json).Groups[1].Value.ToString();re=newRegex("{"NoteID":([^,]*),"NoteCode":"([^"]*)","AddTime":"([^"]*)","OperaterName":"([^"]*)","Type":([^,]*),"Description":"([^"]*)","AuditStatus":([^,]*),"AuditorName":"([^"]*)","RecordAddTime":"([^"]*)","ResellerID":"([^"]*)","ResellerName":"([^"]*)","Amount":"([^"]*)","RecordAddTime"([^"]*)":\[([^\]]*)\]}",RegexOptions.Multiline|RegexOptions.IgnoreCase);MatchCollectionms=re.Matches(json);re=newRegex("{"ProductID":([^,]*),"ProductName":"([^"]*)","OuterCode":"([^"]*)","SKUID":([^,]*),"SKUOuterCode":"([^"]*)","Quantity":([^,]*),"Price":([^,]*),"TotalPrice":([^,]*),"Status":([^}]*)}",RegexOptions.Multiline|RegexOptions.IgnoreCase);Console.WriteLine(ms.Count.ToString());for(inti=0;i<ms.Count;i++){MatchCollectionmsnew=re.Matches(ms[i].Groups[15].Value.Trim());for(intm=0;m<msnew.Count;m++){DataRowdr=table.NewRow();dr["NoteID"]=ms[i].Groups[1].Value.Trim();dr["NoteCode"]=ms[i].Groups[2].Value.Trim();dr["AddTime"]=ms[i].Groups[3].Value.Trim().Substring(0,10);dr["OperaterName"]=ms[i].Groups[4].Value.Trim();dr["Type"]=ms[i].Groups[5].Value.Trim();dr["Description"]=ms[i].Groups[6].Value.Trim();dr["AuditStatus"]=ms[i].Groups[7].Value.Trim();dr["AuditorName"]=ms[i].Groups[8].Value.Trim();dr["RecordAddTime"]=ms[i].Groups[9].Value.Trim();dr["ResellerID"]=ms[i].Groups[10].Value.Trim();dr["ResellerName"]=ms[i].Groups[11].Value.Trim();dr["Amount"]=ms[i].Groups[12].Value.Trim();dr["StockInDetail"]=ms[i].Groups[13].Value.Trim().Substring(0,10);dr["ProductID"]=msnew[m].Groups[1].Value.Trim();dr["ProductName"]=msnew[m].Groups[2].Value.Trim();dr["OuterCode"]=msnew[m].Groups[3].Value.Trim();dr["SKUID"]=msnew[m].Groups[4].Value.Trim();dr["SKUOuterCode"]=msnew[m].Groups[5].Value.Trim();dr["Quantity"]=msnew[m].Groups[6].Value.Trim();dr["Price"]=msnew[m].Groups[7].Value.Trim();dr["TotalPrice"]=msnew[m].Groups[8].Value.Trim();dr["Status"]=msnew[m].Groups[9].Value.Trim();table.Rows.Add(dr);}}}catch(Exceptionex){Console.WriteLine(ex.Message);}}privatevoidbackgroundWorker1_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse){dataGridView1.DataSource=table;MessageBox.Show("数据查询成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);button1.Enabled=true;button2.Enabled=true;}privatevoidbackgroundWorker2_DoWork(objectsender,DoWorkEventArgse){MyExcel.SaveDataTableToExcel(table,saveFileDialog1.FileName,2);}privatevoidbackgroundWorker2_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse){MessageBox.Show("数据导出成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);button1.Enabled=true;button2.Enabled=true;}}}
解决方案
解决方案二:
鬼知道是那一行代码。去学习基本的调试
解决方案三:
现在不显示数据行了,原来有的,2012环境
解决方案四:
re = new Regex("{"NoteID":([^,]*),"NoteCode":"([^"]*)","AddTime":"([^"]*)","OperaterName":"([^"]*)","Type":([^,]*),"Description":"([^"]*)","AuditStatus":([^,]*),"AuditorName":"([^"]*)","RecordAddTime":"([^"]*)","ResellerID":"([^"]*)","ResellerName":"([^"]*)","Amount":"([^"]*)","RecordAddTime"([^"]*)":\[([^\]]*)\]}", RegexOptions.Multiline | RegexOptions.IgnoreCase); MatchCollection ms = re.Matches(json); re = new Regex("{"ProductID":([^,]*),"ProductName":"([^"]*)","OuterCode":"([^"]*)","SKUID":([^,]*),"SKUOuterCode":"([^"]*)","Quantity":([^,]*),"Price":([^,]*),"TotalPrice":([^,]*),"Status":([^}]*)}", RegexOptions.Multiline | RegexOptions.IgnoreCase); Console.WriteLine(ms.Count.ToString()); for (int i = 0; i < ms.Count; i++) { MatchCollection msnew = re.Matches(ms[i].Groups[15].Value.Trim()); for (int m = 0; m < msnew.Count; m++) { DataRow dr = table.NewRow(); dr["NoteID"] = ms[i].Groups[1].Value.Trim(); dr["NoteCode"] = ms[i].Groups[2].Value.Trim(); dr["AddTime"] = ms[i].Groups[3].Value.Trim().Substring(0, 10); dr["OperaterName"] = ms[i].Groups[4].Value.Trim(); dr["Type"] = ms[i].Groups[5].Value.Trim(); dr["Description"] = ms[i].Groups[6].Value.Trim(); dr["AuditStatus"] = ms[i].Groups[7].Value.Trim(); dr["AuditorName"] = ms[i].Groups[8].Value.Trim(); dr["RecordAddTime"] = ms[i].Groups[9].Value.Trim(); dr["ResellerID"] = ms[i].Groups[10].Value.Trim(); dr["ResellerName"] = ms[i].Groups[11].Value.Trim(); dr["Amount"] = ms[i].Groups[12].Value.Trim(); dr["StockInDetail"] = ms[i].Groups[13].Value.Trim().Substring(0, 10);
解决方案五:
你贴这么一坨代码,加上一个不伦不类的帖子标题,别人怎么知道你是神马问题呢?1、具体什么问题,简单描述下2、debug单步调试一下,定位出哪行代码出问题了
解决方案六:
你干嘛不贴整个项目呢,这种问题debug就知道了,先在获取数据的地方加断点,看数据是不是取到了,如果有,再在后面有处理的地方断点,一点点调试下去
解决方案七:
数据获取到了,就是对应不了列
解决方案八:
你取的列不对呗!!!!!!!!!
解决方案九:
莫不是中间加了一列然后就看不到啦