1

I'm trying to make part of a game in which I have to deserialize a JSON into a class and then fill a table from the UI with the retrieved data on the class, such class looks like this:

using System;
[Serializable]
public class Table
{
    public string title;
    public string[] columnHeaders;
    public Data[] data;
}

[Serializable]
public class Data
{
    
}

And this is the current JSON:

{
    "Title": "Team Members",
    "ColumnHeaders": 
    [
        "Id",
        "Name",
    ],
    "Data": 
    [
        {
            "Id": "0",
            "Name": "John Doe",
        },
        {
            "Id": "1",
            "Name": "Jane Doe",
        },
    ]
}

You'll notice that class Data is empty, If I wanted to capture the Data array from the JSON to the class Data I'd have to declare the proper variables for it, string Id and string name, but I want to be able to generate those variables during runtime in a dynamic way so that the table can accept any amount of headers and any type of Data structure, that's my issue/question, what approach is suitable for something like this?

I've been reading a few articles and C# books, about Generic Types, Dictionaries, Dynamic Objects (also ExpandoObjects), ways to deserialize, etc... But no luck so far, or maybe not understanding how to use them properly.

By the way, for the deserialization I'm using JSON.NET, and I fill my class Table with: myTable = JsonConvert.DeserializeObject<Table>(jsonString);, but yeah, the Data will be empty. Found this at some point, sounds like what I want to happen at least:

When deserializing dynamic objects, the serializer first attempts to set JSON property values on a normal .NET member with the matching name. If no .NET member is found with the property name, then the serializer will call SetMember on the dynamic object.

I really hope that there's a way for this, and even if I don't get a literal answer of what I should be doing, being pointed on the right direction about what to study for this kind of things will be really appreciated. Thank you for your time everyone.

Jls
  • 74
  • 5
  • The way to go is knowing what data you can expect, so you can model your classes appropriately. Perhaps a generic Table with T[] Data can work. Otherwise you wouldn't know which (dynamic or not) properties to access. – CodeCaster Dec 16 '20 at 08:17
  • Maybe a simple Dictionary instead of the data class is enough for your use case? A fully runtime extensible data structure is not exactly trivial. – GrimmReaper Dec 16 '20 at 08:33
  • @HimBromBeere, that is not the case for this situation, both columnHeaders and Title work and get the proper data from the JSON. – Jls Dec 16 '20 at 15:50
  • @GrimmReaper, yes, that solved it at the end. Feel kinda silly because I actually tried the dictionary inside of Data class rather than replacing one for another, missed that. Thanks! – Jls Dec 16 '20 at 15:54

1 Answers1

2

You can just replace your empty Data class with Dictionary<string, string> (or Dictionary<string, object>). JSON.net will handle this.

So with this Table class

[Serializable]
public class Table
{
    public string title;
    public string[] columnHeaders;
    public Dictionary<string, string>[] data;
}

you can access your data like this:

var id = tableInstance.data[0]["Id"];
var name = tableInstance.data[0]["Name"];

tableInstance.data[0].Add("Age", "21");
vasily.sib
  • 3,871
  • 2
  • 23
  • 26
  • So I had to replace the entire class with a Dictionary... This is really helpful, had no idea that it would autofill. Thanks a lot, it solved my problem to perfection and taught me something new, thanks for the practical demonstration. – Jls Dec 16 '20 at 17:16