如何在WinForms的DataGridView中显示JSON数据? [英] How to display JSON data in a DataGridView in WinForms?
问题描述
这是我拥有的JSON数据:
This is the JSON data that I have:
{ 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"}
如何在DataGridView中显示它?
How can I show it in a DataGridView?
推荐答案
直接:
- 声明要反序列化为的类。
- 获取Json.NET NuGet包
- 反序列化字符串。
- 绑定
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软件包 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.
这篇关于如何在WinForms的DataGridView中显示JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!