-2

Hello I'm trying to add values to this complex object. I'm trying to turn this object into JSON using JSON.Net.

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

The end result should look something like this: { "Documents": [ { "Title": , "DatePublished": , "DocumentURL": , "ThumbnailURL": , "Abstract": , "Sector": "", "Country": [ "" ], "Document Type": "" } I have tried this but didn't work:

MyList["Dictionary"][0].Add("test", 4);
Kreston
  • 85
  • 1
  • 3
  • 8
  • Please could you post your tried code? what problem you got? – cuongle Apr 08 '13 at 14:37
  • 1
    And why do you need to have a Dictionary of a List of a Dictionary? Isn't there a better, more readable option, maybe? – Conrad Clark Apr 08 '13 at 14:39
  • Just start on the outside an work your way in. Add an item to the outer collection in which you create a collection one level down, then do the same for the next level, and then you can add items to that lowermost level. – Servy Apr 08 '13 at 14:42
  • Sorry for the vagueness this is my first time actually asking a question on here. In the future I will be more descriptive of my problem. Thank you everybody. – Kreston Apr 08 '13 at 15:04
  • what didn't work? the code you have is correct (provided that you have already added a list object to the 'Dictionary' key – Rune FS Apr 08 '13 at 15:05

4 Answers4

3

If you have a Dictionary<string, int> dictA you can add items to this via

Dictionary<string, int> dictA = new Dictionary<string, int>();
dictA.Add("SomeString", 99);

Then to add this dictionary to your List<Dictionary<string, int>> use the following

List<Dictionary<string, int>> dictList = new List<Dictionary<string, int>>();
dictList.Add(dictA);

Now you can add this to your Dictionary<string, List<Dictionary<string, int>>> myList object via

Dictionary<string, List<Dictionary<string, int>>> myList = 
    new Dictionary<string, List<Dictionary<string, int>>>();
myList.Add("SomeString", dictList);

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
2

For what it's worth, here's a sample loop which shows how to use Dictionary.Add and the Collection Initalizer:

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

foreach (int i in Enumerable.Range(1, 100))
{
    var list = new List<Dictionary<string, int>>();
    foreach (int ii in Enumerable.Range(1, 100))
        list.Add(new Dictionary<string, int>() { { "Item " + ii, ii } });
    MyList.Add("Item " + i, list);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
MyList.Add("key1", new List<Dictionary<string, int>());
MyList["key1"].Add(new Dictionary<String, int>());

etc.

Will need more detail from you to give much more than that I'm afraid.

Steve
  • 1,266
  • 16
  • 37
1

Even though it looks complex, it really is quite simple:

Your dictionary takes keys of string, and values of List<Dictionary<string, int>>. So to add a value, that value should always be of type List<Dictionary<string, int>>.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72