Getting Collection text while serializing List<string> inside a Dictionary using JsonConvert in C#

69 views Asked by At

So I have the following data structure defined in my program:

Dictionary<string, Dictionary<List<string>, int>> myTopDict = new Dictionary<string, Dictionary<List<string>, int>>();

Dictionary<List<string>, int> myInnerDict = new Dictionary<List<string>, int>();

int myIntValue=1;

List<string> myListValue=new List<string>();
myListValue.Add("Example Text 1");
myListValue.Add("Example Text 2");

//Here I add to my inner dictionary
myInnerDict.Add(myListValue, myIntValue);

//And finally adding to top dictionary
myTopDict.Add("My Data Set", myInnerDict);

//Serialize here
string result = JsonConvert.SerializeObject(myTopDict);

When I serialize the data structure, I am getting Collection text in the string as shown below:

enter image description here

What am I doing wrong here? Why I am not able to see my data in the serialized result?

1

There are 1 answers

0
Heitor Ribeiro On

For me the main problem that you have in your code is that you are using inside the dictionary as a key a list of string. That's why it's appering like that when you serialize the json

I don't know the expected Json result that you want. But if the idea is to have a collection of values, so change the Dictionary<string, Dictionary<List, int>> to Dictionary<string, Dictionary<string, List>>

Then the serialization will work different

Again, I really don't know the expected output or if that code is what you really want.