C# 如何在C中打印n级(深层)嵌套字典#

C# 如何在C中打印n级(深层)嵌套字典#,c#,C#,如何使用递归函数或其他方法在c#中打印n级深嵌套字典? 这本字典就像 Dictionary<string,object> myDict; 字典myDict; 该对象可以是一个字典n次。 我试过这样的东西 foreach (string key in nestedDict.Keys) { object nextLevel = nestedDict[key]; if(nextLev

如何使用递归函数或其他方法在c#中打印n级深嵌套字典? 这本字典就像

Dictionary<string,object> myDict;

字典myDict;
该对象可以是一个字典n次。 我试过这样的东西

 foreach (string key in nestedDict.Keys)
            {

                object nextLevel = nestedDict[key];

                    if(nextLevel.GetType()== typeof(string))
                    {
                    foreach(var val in nestedDict)
                    {
                        Debug.Log($" the key is  {val.Key}");
                        Debug.Log($" the value is {val.Value}");
                        if(val.Value.GetType()== typeof(Dictionary<,>))
                        {
                            NestedDictIteration((Dictionary<string, object>)val.Value);

                        }

                    }
                    break;
                    }
                    else
                NestedDictIteration((Dictionary<string, object>)nextLevel);
            }
foreach(nestedDict.Keys中的字符串键)
{
object nextLevel=nestedDict[key];
if(nextLevel.GetType()==typeof(字符串))
{
foreach(nestedDict中的var val)
{
Log($“密钥为{val.key}”);
Log($“值为{val.value}”);
if(val.Value.GetType()==typeof(字典))
{
NestedDictIteration((字典)val.Value);
}
}
打破
}
其他的
嵌套字典迭代((字典)下一级);
}
问候
GM

编写递归方法相当简单,我添加了一个
level
参数,只是为了让树更容易显示:

static void NestedPrint(Dictionary<string,object> dict, int level = 0)
{
    foreach(var item in dict)
    {
        if(item.Value is Dictionary<string,object> nested)
        {
            Console.WriteLine($"{new string(' ',level)}{item.Key}:");
            NestedPrint(nested,level+1);
        }
        else
        {
            Console.WriteLine($"{new string(' ',level)}{item.Key} = {item.Value}");
        }
    }
}
static void NestedPrint(字典目录,int级别=0)
{
foreach(dict中的var项目)
{
if(item.Value是字典嵌套的)
{
Console.WriteLine($“{newstring('',level)}{item.Key}:”);
嵌套打印(嵌套,级别+1);
}
其他的
{
WriteLine($“{newstring('',level)}{item.Key}={item.Value}”);
}
}
}

实例:

编写递归方法相当简单,我添加了一个
级别
参数,只是为了让树更容易显示:

static void NestedPrint(Dictionary<string,object> dict, int level = 0)
{
    foreach(var item in dict)
    {
        if(item.Value is Dictionary<string,object> nested)
        {
            Console.WriteLine($"{new string(' ',level)}{item.Key}:");
            NestedPrint(nested,level+1);
        }
        else
        {
            Console.WriteLine($"{new string(' ',level)}{item.Key} = {item.Value}");
        }
    }
}
static void NestedPrint(字典目录,int级别=0)
{
foreach(dict中的var项目)
{
if(item.Value是字典嵌套的)
{
Console.WriteLine($“{newstring('',level)}{item.Key}:”);
嵌套打印(嵌套,级别+1);
}
其他的
{
WriteLine($“{newstring('',level)}{item.Key}={item.Value}”);
}
}
}

实例:

欢迎来到StackOverflow!请阅读。特别是,请你的问题包括你已经尝试过的细节。首先:你的递归思想不错。您只需要了解递归是如何工作的(您是否已经尝试过了?)。你必须明白,字典本身并不知道它是哪一级。这是递归方法中的任务。我添加了我尝试使用的部分函数欢迎使用StackOverflow!请阅读。特别是,请你的问题包括你已经尝试过的细节。首先:你的递归思想不错。您只需要了解递归是如何工作的(您是否已经尝试过了?)。你必须明白,字典本身并不知道它是哪一级。这是递归方法中的任务。我添加了我正在尝试的分部函数