这是我拥有的 JSON 数据:
{"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 中显示它?
最佳答案
所以这很简单:
- 声明一个要反序列化的类。
- 获取 Json.NET NuGet 包。
- 反序列化字符串。
- 绑定(bind)
DataGridView
。
声明一个反序列化的类
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 .
反序列化字符串
var result = JsonConvert.DeserializeObject<List<JsonResult>>(input);
绑定(bind)DataGridView
dataGridView.DataSource = result;
注意:这是绑定(bind)到网格的最原始的方式。您可以利用更多选项。我想到的一个是,关闭 AutoGenerateColumns
并定义您自己的列;设计师驱动的工作,因此它不会影响我提供的代码。
关于c# - 如何在 WinForms 的 DataGridView 中显示 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23763446/