json数组到c#windows窗体表

json数组到c#windows窗体表,c#,json,C#,Json,我正在尝试构建C#Windows窗体应用程序API以与web服务通信。在API中,我将cs文件作为带有函数的类文件内容。有一个从web服务获取响应的函数,输出是JSON。我希望将这个类文件Json输出到另一个windows窗体作为表 类函数如下所示: JavaScriptSerializer jsonSer = new JavaScriptSerializer(); Dictionary<String, object> jsonParams = new Dictionary<s

我正在尝试构建C#Windows窗体应用程序API以与web服务通信。在API中,我将cs文件作为带有函数的类文件内容。有一个从web服务获取响应的函数,输出是JSON。我希望将这个类文件Json输出到另一个windows窗体作为表

类函数如下所示:

JavaScriptSerializer jsonSer = new JavaScriptSerializer();
Dictionary<String, object> jsonParams = new Dictionary<string, object>();

jsonParams.Add("within", guest.within);

m_currentCommand = "json=";
m_currentCommand = jsonSer.Serialize(jsonParams);

jsonParams.Clear();

// Create the request and setup for the post.
string strRequest = string.Format("{0}api/s/{1}/stat/guest", Server, SiteName);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strRequest);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = WebRequestMethods.Http.Post;
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.CookieContainer = m_UBNTCookies;

// Set the callback to handle the post of data.
httpWebRequest.BeginGetRequestStream(new AsyncCallback(PostCallback), httpWebRequest);

//Wait for the send to complete
m_allDone.WaitOne();

// Now read the reponse and process the data.
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    string responseText = streamReader.ReadToEnd();
    //DataSet obj = JsonConvert.DeserializeObject(responseText, (typeof(DataTable)));
    //IDictionary<int, ICollection<guestdet>> guests;
    //guests = JsonConvert.DeserializeObject<IDictionary<int, ICollection<guestdet>>>(responseText);
    //DataTable dtValue = (DataTable)JsonConvert.DeserializeObject(responseText, (typeof(DataTable)));
    jsonParams = jsonSer.Deserialize<Dictionary<String, object>>(responseText);

    if (!jsonParams.ContainsKey("data"))
    {
        guest.ERROR = "Error getting guest details";
        return ReturnValue;
    }

     ArrayList list = (ArrayList)jsonParams["data"];


    string strData = string.Empty;

    foreach (Dictionary<String, object> data in list)
    {

        guest.vouchercode = data["voucher_code"].ToString();
        guest.vouchercode = Left(guest.vouchercode, 5) + "-" + Right(guest.vouchercode, 5);
        guest.mac = data["mac"].ToString();
        guest.hostname = data["hostname"].ToString();
        guest.ip = data["ip"].ToString();
        guest.download = data["tx_bytes"].ToString();
        guest.upload = data["rx_bytes"].ToString();

        ReturnValue = true;
    }
}
JavaScriptSerializer jsonSer=newJavaScriptSerializer();
Dictionary jsonParams=新字典();
jsonParams.Add(“in”,guest.in);
m_currentCommand=“json=”;
m_currentCommand=jsonSer.Serialize(jsonParams);
jsonParams.Clear();
//为帖子创建请求和设置。
string strequest=string.Format(“{0}api/s/{1}/stat/guest”,服务器,站点名);
HttpWebRequest HttpWebRequest=(HttpWebRequest)WebRequest.Create(strRequest);
httpWebRequest.ContentType=“应用程序/x-www-form-urlencoded”;
httpWebRequest.Method=WebRequestMethods.Http.Post;
httpWebRequest.ServicePoint.Expect100Continue=false;
httpWebRequest.CookieContainer=m_UBNTCookies;
//设置回调以处理数据的post。
httpWebRequest.BeginGetRequestStream(新的异步回调(后回调),httpWebRequest);
//等待发送完成
m_all done.WaitOne();
//现在读取响应并处理数据。
HttpWebResponse httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
使用(StreamReader StreamReader=newstreamreader(httpResponse.GetResponseStream())
{
字符串responseText=streamReader.ReadToEnd();
//DataSet obj=JsonConvert.DeserializeObject(responseText,(typeof(DataTable));
//我的来宾;
//guests=JsonConvert.DeserializeObject(responseText);
//DataTable dtValue=(DataTable)JsonConvert.DeserializeObject(responseText,(typeof(DataTable));
jsonParams=jsonSer.Deserialize(responseText);
如果(!jsonParams.ContainsKey(“数据”))
{
guest.ERROR=“获取客人详细信息时出错”;
返回值;
}
ArrayList=(ArrayList)jsonParams[“数据”];
string strData=string.Empty;
foreach(列表中的字典数据)
{
guest.vouchercode=数据[“凭证代码”].ToString();
guest.vouchercode=Left(guest.vouchercode,5)+“-”+Right(guest.vouchercode,5);
guest.mac=data[“mac”].ToString();
guest.hostname=data[“hostname”].ToString();
guest.ip=data[“ip”].ToString();
guest.download=data[“tx_字节”].ToString();
guest.upload=data[“rx_字节”].ToString();
ReturnValue=true;
}
}

到底是什么问题?它是否没有给出所需的输出(如果是,请提供您想要的和正在发生的事情的示例),它在运行时是否崩溃(在哪一行),它是否未编译(提供错误消息)?您的问题不是很清楚,但无论如何,我们不会为您做工作。你们已经试过什么了?我想得到上面的responseText到表数组键作为表头,值作为表体。现在我可以把它们放到文本框中。