如何在 WinForms 的 DataGridView 中显示 JSON 数据? [英] How to display JSON data in a DataGridView in WinForms?

查看:32
本文介绍了如何在 WinForms 的 DataGridView 中显示 JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的 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"}

如何在 DataGridView 中显示它?

How can I show it in a DataGridView?

推荐答案

所以这很简单:

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

绑定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屋!

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