I need to create an array of dictionaries where the size of this array can vary dynamically.
In this example I create an array of 10 elements and only fill 2. But I need the array to get bigger or smaller depending on the code.
Dictionary<string, object>[] valor = new Dictionary<string, object>[10];
To fill it:
valor[0].Add("llave1","01");
valor[0].Add("llave2","02");
valor[1].Add("llave1","11");
valor[1].Add("llave2","12");
If we were to do the equivalence to a JSON, the structure could be like this:
{
"informe":[{ "grupo1" : [{
"llave1": "01",
"llave2": "02"
},
{
"llave1": "11",
"llave2": "12"
}]
},
{ "grupo2" : [{
"llave1": "54",
"llave2": "62"
},
{
"llave1": "35",
"llave2": "23"
}]
}]
}
IMPORTANT: What I need is to declare the elements dynamically.
If we use
http://json2csharp.com/
you'll be able to get the c# structure for that
json
, which doesn't need anyDictionary<>
But perhaps the
Grupo1
andGrupo2
could be defined simply asGrupo
The idea is that using the library
Newtonsoft.Json
deserialize that json to the class using
Remember that the json has a structure that you must respect, you cannot apply properties such as keys as if they were the key of a dictionary, that does not work
If you are going to serialize a dictionary
Serialize to Dictionary
you could make one contain the other like being
If you want to add something additional you would use
You can use lists, dictionaries, and a wrapper class.
First the class that will do the wrapper:
It is an extremely simple class, it only has one property that is
informe
and a constructor that initializes said property.Now I place how to fill or use the class
CustomWrap
:Which the output or the value of the variable
json
is:For each group a dictionary is created where its value will be a list. Also note that within the class
CustomWrap
you can create methods to add groups and keys, always validating the keys added to the dictionaries so that they are not repeated and exceptions are thrown.The code that I leave you does not mean that it is the most elegant solution, since it becomes a bit tedious and confusing to see a dictionary within lists and lists within a dictionary. To give it a bit more clarity (as I put in the previous paragraph), create your own add, delete and edit methods. Below I leave the list and dictionary documentation links where you can find more information on how to add and delete.
Something else that can be identified is that for each group a dictionary is created which will only contain a key with its value, which for me is wasting the use of a dictionary, although for the example it works perfectly. I advise you that if you can change the json structure to a much simpler one, it will make your job easier.
In the example I use
JsonConvert.SerializeObject
to obtain the json, this library can be downloaded from the NuGet package manager. Below I leave the link to the library.It occurs to me that you create a method to perform the increase or decrease, I leave you an example.
It may not be the most elegant way, but it's what occurred to me at the time.
Otherwise use