c# - 如何在 WinForms 的 DataGridView 中显示 JSON 数据?

标签 c# json winforms datagridview deserialization

这是我拥有的 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 中显示它?

最佳答案

所以这很简单:

  1. 声明一个要反序列化的类。
  2. 获取 Json.NET NuGet 包。
  3. 反序列化字符串。
  4. 绑定(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/

相关文章:

c# - winforms应用程序中存储数据库访问密码的位置

JavaScript 和 Node.js - Controller 函数中的 JSON 字符串不正确

c# - 在 MVP Winforms App 中共享模型

c# - 为什么即使在 Azure 中配置所需的设置后,我也看不到应用服务中的日志?

c# - 与 C++/Win32 中的错误代码相比,为什么异常在 C#/.NET 编程中如此流行?

php - Laravel 的 TestCase 方法 seeJson() 只查找数组

java - PlayFramework - 使用 json 渲染运行功能测试

c# - 从 PictureBox 中的鼠标位置获取真实图像坐标

C# postback dropdownlist 总是选择第一个值

c# - 如何将 DateTimePicker 值设置为今天的日期+1