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.