如何在c#windows中的dataGridView中显示Json DATA来自HttpWebResponse的应用程序 [英] How To Display the Json DATA in dataGridView in c# windows Application from HttpWebResponse

查看:1446
本文介绍了如何在c#windows中的dataGridView中显示Json DATA来自HttpWebResponse的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json数据,我从 HTTPWebResponse

The json data , I am getting from HTTPWebResponse


[{ testId:1,testName:HTML,minScore:20,score:40,date:12-2-2014,status:PASSED},{testId :1,testName:JAVA,minScore:20,score:10,date:12-2-2014,status:FAILED}]

[{"testId":1,"testName":"HTML","minScore":20,"score":40,"date":"12-2-2014","status":"PASSED"},{"testId":1,"testName":"JAVA","minScore":20,"score":10,"date":"12-2-2014","status":"FAILED"}]







HttpResponseMessage Response = await client.PostAsync("http://motivtechindia.com/quiz/ws/empquizs", content);
int statusCode = (int)Response.StatusCode;
string results = await Response.Content.ReadAsStringAsync();
MessageBox.Show(results);`enter code here`

___________________________________________________________________________________________消息框显示:

___________________________________________________________________________________________the message box shows:


[{testId:1,testName:HTML,minScore:20,score date:12-2-2014,status:PASSED},{testId:1,testName:JAVA,minScore:20,score:10,date :12-2-2014,status:FAILED}]

[{"testId":1,"testName":"HTML","minScore":20,"score":40,"date":"12-2-2014","status":"PASSED"},{"testId":1,"testName":"JAVA","minScore":20,"score":10,"date":"12-2-2014","status":"FAILED"}]






现在请告诉我如何在datagridview中进行解析和显示!!!


Now Please tell me how to deserilise and show it in datagridview!!!

推荐答案

所以这是非常简单的:

So this is pretty straight forward:


  1. 声明一个反序列化的类。

  2. 获取Json.NET NuGet软件包。 li>
  3. 反序列化字符串。

  4. 绑定 DataGridView

  1. Declare a class to deserialize into.
  2. Grab the Json.NET NuGet Package.
  3. Deserialize the string.
  4. Bind the DataGridView.



声明一个类来反序列化为



Declare a class to deserialize into

public class JsonResult
{
    public int testId { get; set; }
    public string testName { get; set; }
    public int minScore { get; set; }
    public int score { get; set; }
    public DateTime date { get; set; }
    public string status { get; set; }
}



获取Json.NET NuGet软件包



从这里 Json.NET NuGet软件包/6.0.3rel =noreferrer> http://www.nuget.org/packages/Newtonsoft.Json/6.0.3

Grab the Json.NET NuGet Package

Pull the Json.NET NuGet Package in from here http://www.nuget.org/packages/Newtonsoft.Json/6.0.3.

var result = JsonConvert.DeserializeObject<List<JsonResult>>(input);



绑定 DataGridView



Bind the DataGridView

dataGridView.DataSource = result;

注意:这是绑定到网格的最原始的方式。还有更多的选择可以利用。需要注意的是,关闭 AutoGenerateColumns 并定义自己的列;设计师驱动的工作,所以它不会影响我提供的代码。

NOTE: this is the most primitive way of binding to the grid. There are many more options that you can leverage. One that comes to mind is, turning off AutoGenerateColumns and defining your own columns; designer-driven work so it wouldn't affect the code I've provided.

这篇关于如何在c#windows中的dataGridView中显示Json DATA来自HttpWebResponse的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆