在 C# 中以 windows 形式显示我的 .json 文件中的数据

Displaying data from my .json file in a windows form in C#

过去几天我一直在阅读有关此问题的文章,并且有很多类似的问题,但 none 帮助了我。

我的 .json 文件中有我的数据,格式如下:

{
  "Run from 27.09.2021 19:47:22": {
    "decksize": 52,
    "correctcards": 23,
    "time2mem": "00:00:5434",
    "time2sor": "00:00:4214",
    "mempalaces": [ "none" ],
    "troublecards": [ "none" ]
  }
  ,
  "Run from 27.09.2021 19:47:29": {
    "decksize": 32,
    "correctcards": 1,
    "time2mem": "00:00:5628",
    "time2sor": "00:00:5724",
    "mempalaces": [ "none" ],
    "troublecards": [ "none" ]
  },
  "Run from 27.09.2021 19:47:36": {
    "decksize": 32,
    "correctcards": 11,
    "time2mem": "00:00:6224",
    "time2sor": "00:00:5201",
    "mempalaces": [ "none" ],
    "troublecards": [ "none" ]
  }
}

我想在数据网格视图或其他任何最简单的方法中显示它(如果列表框更简单,那就这样吧)。

我曾尝试使用 Newtonsoft.Json 将其反序列化为一个对象,但我希望找到一种仅使用 System.Text.JsonSystem.Text.Json.Serialization 的方法,因为这两个在我的工作中对我有用项目。

我目前正在像这样反序列化:data = JsonSerializer.Deserialize<values_advanced>(jsonString); 并像这样绑定我的数据源:dbHistory.DataSource = data; 但是当我启动我的程序时网格是空的。

我也试过像这样将 jsonString 转换成 DataTable:var data2 = JsonConvert.DeserializeObject<List<values_advanced>>(jsonString); 但我遇到了一个错误,声称 NewtonSoft 运行 变成了并发症。我粘贴了整个错误 here.

任何帮助将不胜感激,因为我现在大部分空闲时间都花在了这个问题上,它开始困扰我。

我附上了来自 Excel 的屏幕截图,以可视化我希望如何可视化数据: data

注意事项:您的 JSON 结构的外部类型相当于 C# 中的字典。您不能将字典类型用作视图的数据源。您需要将字典转换为 IEnumerable,一个简单的方法是通过 LINQ:

Dictionary<string, RunData> dict = JsonConvert.Deserialize<Dictionary<string, RunData>>(jsonString);
IEnumerable<RunData> data = dict.Values.ToList();
...

创建 data 后,您可以将 DataGridView 的数据源设置为可枚举的。

尝试使用 System.Text.Json


var list = JsonSerializer.Deserialize<Dictionary<string,Data>>(json).Values.ToList();

但是您需要将数组转换为字符串才能将其用作网格视图的数据源,因此

List<GvData> dataSource = list.Select(v => new GvData
    {
        Decksize = v.Decksize,
        Correctcards = v.Correctcards,
        Time2Mem = v.Time2Mem,
        Time2Sor = v.Time2Sor,
        Mempalaces = string.Join(",", v.Mempalaces),
        Troublecards = string.Join(",", v.Troublecards)
    }).ToList();
.....

dbHistory.DataSource = dataSource ;

public partial class Data
{
    [JsonPropertyName("decksize")]
    public long Decksize { get; set; }

    [JsonPropertyName("correctcards")]
    public long Correctcards { get; set; }

    [JsonPropertyName("time2mem")]
    public string Time2Mem { get; set; }

    [JsonPropertyName("time2sor")]
    public string Time2Sor { get; set; }

    [JsonPropertyName("mempalaces")]
    public string[] Mempalaces { get; set; }

    [JsonPropertyName("troublecards")]
    public string[] Troublecards { get; set; }
}

public partial class GvData
{
    public long Decksize { get; set; }
    public long Correctcards { get; set; }
    public string Time2Mem { get; set; }
    public string Time2Sor { get; set; }
    public string Mempalaces { get; set; }
    public string Troublecards { get; set; }
}

这应该有所帮助。首先,您需要一个 class 来描述您的格式...

[Serializable()]
    public class MyRuns
    {
        public string Run_Name { get; set; }
        public int decksize { get; set; }
        public int correctcards { get; set; }
        public string time2mem { get; set; }
        public string time2sor { get; set; }
        public string mempalaces { get; set; }
        public string troublecards { get; set; }
    }
using System.Text.Json;

我不得不对你的 json 做一些细微的改动,但这很有效...

string strJson = @"[{

    ""Run_Name"": ""Run from 27.09.2021 19:47:22"",
    ""decksize"": 52,
    ""correctcards"": 23,
    ""time2mem"": ""00:00:5434"",
    ""time2sor"": ""00:00:4214"",
    ""mempalaces"": ""none"",
    ""troublecards"": ""none""
        },
{
    ""Run_Name"": ""Run from 27.09.2021 19:47:29"",
    ""decksize"": 32,
    ""correctcards"": 1,
    ""time2mem"": ""00:00:5628"",
    ""time2sor"": ""00:00:5724"",
    ""mempalaces"": ""none"",
    ""troublecards"": ""none""
    },
  {
    ""Run_Name"": ""Run from 27.09.2021 19:47:36"",
    ""decksize"": 32,
    ""correctcards"": 11,
    ""time2mem"": ""00:00:6224"",
    ""time2sor"": ""00:00:5201"",
    ""mempalaces"": ""none"",
    ""troublecards"": ""none""
    }
]";

List<MyRuns> myRuns = JsonSerializer.Deserialize<List<MyRuns>>(strJson);

            foreach(var r in myRuns)
            {
                Console.WriteLine("--------------------------------------");
                Console.WriteLine("Run Name: " + r.Run_Name);
                Console.WriteLine("decksize: " + r.decksize);
                Console.WriteLine("correctcards: " + r.correctcards.ToString());
                Console.WriteLine("time2mem: " + r.time2mem.ToString());
                Console.WriteLine("time2sor: " + r.time2sor.ToString());
                Console.WriteLine("mempalaces: " + r.mempalaces);
                Console.WriteLine("troublecards: " + r.troublecards);
                Console.WriteLine("----------------------------------------");
            }

输出:

--------------------------------------
Run Name: Run from 27.09.2021 19:47:22
decksize: 52
correctcards: 23
time2mem: 00:00:5434
time2sor: 00:00:4214
mempalaces: none
troublecards: none
----------------------------------------
--------------------------------------
Run Name: Run from 27.09.2021 19:47:29
decksize: 32
correctcards: 1
time2mem: 00:00:5628
time2sor: 00:00:5724
mempalaces: none
troublecards: none
----------------------------------------
--------------------------------------
Run Name: Run from 27.09.2021 19:47:36
decksize: 32
correctcards: 11
time2mem: 00:00:6224
time2sor: 00:00:5201
mempalaces: none
troublecards: none
----------------------------------------