Issues Iterating through a dynamic object generated from a JSON file
<p>I have a json file that (for the sake of this question) I simplified down:</p>

<pre><code>{
  "servername": {
    "goodvolumes": [
      {
        "Name": "vol1",
        "State": "online",
        "Size": "12.0 TB"
      },
      {
        "Name": "vol2",
        "State": "online",
        "Size": "10.0 TB"
      }
    ],
    "BadVolumes": {
      "Name": "badVol",
      "State": "offline",
      "TotalSize": "120GB"
    }
  }
}
</code></pre>

<p>When this is being read into my C# I have an data object of type <code>System.Collections.Generic.Dictionary&lt;string,object&gt;</code>.</p>

<p>I am then iterating through the object and creating a model object that I am going to be passing to my view.</p>

<p>This is posing a difficulty for me. Here is an example of how I am iterating through the json.</p>

<pre><code>//top level of my JSON - the serverName
foreach(serverName in jsonData) 
{
    //record the serverName

    //second level of my JSON - the notification name
    foreach(notification in serverName.Value)
    {
        //record the notification name

        //3rd Level of my JSON - iterating the entries
        foreach(entry in notification.Value)
        {
            //Iterating all the values in an entry
            foreach(entryValue in entry)
            {
               //record values in each entry
            }

        }
    }
}
</code></pre>

<p>The issue that I am having is when iterating through the third level if there is only 1 entry.</p>

<p>By the nature of JSON, if a notification type has multiple entries, then inside of my <code>notifications.Value</code> there will be another list of collections. In this case, my code works like a charm.</p>

<p>However, if there is only 1 entry for a notification,<code>notification.value</code> actually contains the list of <code>KeyValuePair</code> for all of the values inside the single entry. So the 3rd level of iteration isn't working. It is actually trying to iterate through the values at that point.</p>

<p>Unfortunately the json I am working with cannot be modified, this is the format that I am going to be receiving it in.</p>

<p>I hope that this accurately explains the issue that I am having. I know where the issue occurs, I am just not sure at all how to get around it.</p>