0

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?

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • 3
    Keys in Json have to be a string, you are trying to use a `List`. Are you sure you didn't mean `Dictionary>>`? – Yitz Sep 05 '22 at 21:02
  • can't you just use an array instead of a List inside the value? – Felipe Esteves Sep 05 '22 at 21:03
  • @Yitz Will it make any difference? Can you give an example? – Rahul Sharma Sep 05 '22 at 21:05
  • @FelipeEsteves What will that do in this case? Any example you can provide of using arrays over List will give the desired result? – Rahul Sharma Sep 05 '22 at 21:05
  • 1
    @RahulSharma what's the expected JSON output here? Can you give an example of what you want the serialized JSON to look like? – HasaniH Sep 05 '22 at 21:07
  • 3
    As mentioned by @Yitz, `Dictionary, int> myInnerDict` is a dictionary with **complex keys**. If this is not a typo and you instead meant `Dictionary>>`, then Json.NET does not serialize such dictionaries out of the box, so you will need to implement a custom `JsonConverter` or `TypeConverter`. For details see [Not ableTo Serialize Dictionary with Complex key using Json.net](https://stackoverflow.com/q/24504245/3744182). In fact that may be a duplicate. Does it answer your question? – dbc Sep 05 '22 at 21:08
  • If I use @Yitz structure, I get the error that a key with the same value has already been added in the dictionary. The integer value is cannot be the key since it varies as per my program. I am looking into the custom converter as dbc suggested. – Rahul Sharma Sep 05 '22 at 21:10
  • 1
    See also [How can I serialize/deserialize a dictionary with custom keys using Json.Net?](https://stackoverflow.com/q/24681873). – dbc Sep 05 '22 at 21:15
  • 1
    @dbc This converter worked: https://stackoverflow.com/a/27043792/1807452 . Thanks for the assist. – Rahul Sharma Sep 05 '22 at 21:22
  • I'm surprised that a Dictionary whose key type is `List`. What does key equality mean for two different `List` instances? How is a `List` hashed? Maybe I _just works_, but it certainly wouldn't show up in code I wrote (even ignoring the JSON issues) – Flydog57 Sep 05 '22 at 21:25
  • @Flydog57 All the 'messy' structure goes to the front-end where it is parsed by Javascript to generate dynamic data on the `View`. – Rahul Sharma Sep 05 '22 at 21:31

1 Answers1

-1

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.