I'm trying to deseralize a json file into a C# object using Newtonsoft, but it's not transferring into a list properly.
My object is this:
public class MaterialTemplate : INamed
{
public string name = "defaultMTemplate";
List<string> essential = new List<string>();
List<string> required = new List<string>();
List<string> forbidden = new List<string>();
public List<MaterialData> FindMaterials()
{
return DataController.SearchByTag<MaterialData>(essential, required, forbidden);
}
public string OutputData()
{
string output = $"Name: {name}; ";
output += "Required: ";
foreach(string tag in required)
{
output += tag + ";";
}
output += " Essential: ";
foreach (string tag in essential)
{
output += tag + ";";
}
output += " Forbidden: ";
foreach (string tag in forbidden)
{
output += tag + ";";
}
return output;
}
public string GetName()
{
return name;
}
}
and the JSON I am trying to deseralize is:
{"name": "normalFloor", "essential": ["floor"], "required": ["wood", "tile", "carpet"]}
I'm expecting the lists to be populated from the JSON file, but instead I get "Name: normalFloor; Required: Essential: Forbidden:" from OutputData. I don't see where I could have gone wrong in the formatting, and as you see, the object is being created, but the lists are empty.
I've tried changing these lists to arrays but that didn't work either.
publicfor Json.NET to populate them, e.g.public List<string> essential = new List<string>();publicadded to your lists here: dotnetfiddle.net/1r7hSu. Does that answer your question, or do you need additional help? If you need additional help please edit your question to share a minimal reproducible example showing your remaining problem(s). Thanks!