I have to create a json file with an array inside in c# so this is my code but throws an exception:
Method name expected
before new Dictionary<string, int>[2]();
Have you got any suggestions?
int[] lung = results.ToArray();
Dictionary<string, Dictionary<string, int>[]> padre = new Dictionary<string, Dictionary<string, int>[]>();
Dictionary<string, int>[] udemy = new Dictionary<string, int>[2]();
padre.Add("Lunghezza", udemy);
udemy[0].Add("attributo", lung[0]);
udemy[1].Add("attributo2", lung[1]);
string js = JsonConvert.SerializeObject(padre);
System.Diagnostics.Debug.WriteLine(js);
This isn't how you construct an array;
new Dictionary<string, int>[2]()
You don't need the ()
at all - you're not calling a method or a constructor; you're creating an array instance. Just:
new Dictionary<string, int>[2]
You'll need to populate the array as well, of course, e.g.
udemy[0] = new Dictionary<string, int>();
udemy[1] = new Dictionary<string, int>();
Using a simpler array type makes it easier to see the problem:
// Invalid
int[] x = new int[2]();
// Valid
int[] x = new int[2];
As a side note, a type of Dictionary<string, Dictionary<string, int>[]>
is pretty horrible - I would strongly encourage you to redesign that to use more meaningful types.
See more on this question at Stackoverflow