json数组到c#windows表格

时间:2017-03-02 05:54:23

标签: c# json

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

类函数如下:

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;
    }
}

0 个答案:

没有答案