While messing around with a class which I'd like to serialise into a JSON, I encountered a situation which I'm not sure how to best resolve. It has to do with a JSON structure that has both fixed and random attributes (Fixed: always the same attribute name, Random: their names are random). Also, the fixed attributes are always of type string
and the random attributes are always of type MyJsonClass2
.
For example:
{
"fixedName1": "value1",
"fixedName2": "value2",
"randomName1": { ... },
"randomNameN": { ... }
}
This can be represented using a Dictionary<string, object>
. However because the fixedName1
and fixedName2
are always given, I'd like to have them as fixed declarations and only have the variables in a dictionary as follows:
class MyJson
{
public string fixedName1 = "blah1";
public string fixedName2 = "blah2";
public Dictionary<string, MyJsonClass2> randomNames = new Dictionary<string, MyJsonClass2>();
}
The problem here is that when this is serializsed, the values from randomNames
are enclosed in an attribute called randomNames, which makes sense, however this is not what I'd like to see. Is there are way to tell the Newtonsoft JSON serializer to ignore the dictionary name? Or what is the best way to define such a class?