Reading values inside Dictionary<string, object>

Viewed 141

I am using Editor Datatables and am returning data from Editor to a controler and am trying to read the values from a so-called Dictionary<string,object>. Can anyone advise how this is done. I read that the Dictionary needs to be Json deserialized but am not too sure how it's done. I'm including a picture of the values I'm trying to read. Thanks.enter image description here

I read in the controller the data returned via e.Values:

editor.PostCreate += (sender, e) => t = Task.Run(() => Add(intContTpe, lngContIdx, e.Values));
internal static void Add(int intContTpe, long lngContIdx, object objPCEAValues)
{
    Dictionary<string, object> dicVoDaCDRFormats = new Dictionary<string, object>();
    if (!dicPCEAValues.ContainsKey("CustomerVoiceCLICDRFormats.0"))
    {
        dicVoDaCDRFormats = ((Dictionary<string, object>)dicPCEAValues["CustomerVoiceCLICDRFormats"]);

        PrintToOutput("", ((Dictionary<string, object>)dicPCEAValues["CustomerVoiceCLICDRFormats"])["0"]);
    }
}
1 Answers

Each value in the dicVoDaCDRFormats is a dictionary it self. You need to open those dictionaries as well.

Check if this gives you the result intended.

foreach (KeyValuePair<string, object> items in dicVoDaCDRFormats) 
{
    if (items.Value is Dictionary<string, object> innerDict)
        foreach (var item in innerDict)
            PrintToOutput("", items.Value); 
}

These kind of working with the object type is not safe. You can add type checking for other parts with the is and as keywords to be safe. But it depends on the case you are working with.

Related