I have been reading for the last couple days about this issue and have many similar questions but none have helped me.
I have my data inside my .json file in the following format:
{
"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" ]
}
}
and I want to display it in a datagridview or whatever else is the easiest way (if a listbox is easier then so be it).
I have tried to deserialize it into an object using Newtonsoft.Json
but I was hoping to find a way using just System.Text.Json
and System.Text.Json.Serialization
as these two have worked for me in my project.
I am currently deserializing like this: data = JsonSerializer.Deserialize<values_advanced>(jsonString);
and binding my data source like this: dbHistory.DataSource = data;
but the grid is just empty when I start my program.
I also tried converting the jsonString into a DataTable like this: var data2 = JsonConvert.DeserializeObject<List<values_advanced>>(jsonString);
but I encountered an error claiming that NewtonSoft ran into complications. I pasted the whole error here.
Any help would be greatly appreciated as I have spent most of my free time on this issue now and it is starting to bug me.
I have included a screenshot from Excel to visualize how I want the data to be visualized: data
CodePudding user response:
Some notes: your JSON structure's outer type is equivalent to a Dictionary in C#. You cannot use a Dictionary type as a view's DataSource. You will need to transform your dictionary into an IEnumerable
and an easy way to do so is via LINQ:
Dictionary<string, RunData> dict = JsonConvert.Deserialize<Dictionary<string, RunData>>(jsonString);
IEnumerable<RunData> data = dict.Values.ToList();
...
After creating data
you can set your DataGridView's DataSource to that enumerable.
CodePudding user response:
This should help. First, you need a class that describes your format...
[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;
I had to make a couple of slight changes to your json, but this works...
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("----------------------------------------");
}
Output:
--------------------------------------
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
----------------------------------------
CodePudding user response:
try this using System.Text.Json
var list = JsonSerializer.Deserialize<Dictionary<string,Data>>(json).Values.ToList();
but you neeed to convert arrays to string in order to use it as data source for Grid View, so
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 ;
classes
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; }
}