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

查看:171
本文介绍了如何在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}

{"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

  1. Declare a class to deserialize into.
  2. Grab the Json.NET NuGet Package.
  3. Deserialize the string.
  4. 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屋!

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