I have a json file that (for the sake of this question) I simplified down:
{
"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"
}
}
}
When this is being read into my C# I have an data object of type System.Collections.Generic.Dictionary<string,object>
.
I am then iterating through the object and creating a model object that I am going to be passing to my view.
This is posing a difficulty for me. Here is an example of how I am iterating through the json.
//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
}
}
}
}
The issue that I am having is when iterating through the third level if there is only 1 entry.
By the nature of JSON, if a notification type has multiple entries, then inside of my notifications.Value
there will be another list of collections. In this case, my code works like a charm.
However, if there is only 1 entry for a notification,notification.value
actually contains the list of KeyValuePair
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.
Unfortunately the json I am working with cannot be modified, this is the format that I am going to be receiving it in.
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.