如何在c#windows中的dataGridView中显示Json DATA来自HttpWebResponse的应用程序 [英] How To Display the Json DATA in dataGridView in c# windows Application from 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:
- 声明一个反序列化的类。
- 获取Json.NET NuGet软件包。 li>
- 反序列化字符串。
- 绑定
DataGridView
。
- Declare a class to deserialize into.
- Grab the Json.NET NuGet Package.
- Deserialize the string.
- 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屋!