最有效的平展嵌套字典的方法

本文关键字:字典 方法 嵌套 有效 | 更新日期: 2023-09-27 18:06:22

问题在于将Key字符串构建为名称空间。这样做与递归是我目前的实现,但我相信有更多的堆栈友好的选项(LINQ?迭代?),我还没能找到。几乎每个例子都太简单,没有考虑到基于键层次结构的"命名空间"的能力。

这是我的字典布局的一个快速示例。希望这足够容易理解-我想要彻底。

我将JSON转换为类似于这样(嵌套,以便在网络上保存数据):

"entity": {
    "foo": {
      "bar": {
        "baz": {
          "2": "description",
          "1": "title"
             }
          }

进入Dictionary<string,object>。当Value is string时,"命名空间"结束。对这个对象的详细的、令人困惑的观察:

[0] {[entity, Dictionary[String,Object]]}   KeyValuePair<string,object>
  Key   "entity"    string
  Value Count = 1   object {Dictionary<string,object>}
    [0] {[foo, Dictionary[String,Object]]}  KeyValuePair<string,object>
    Key "foo"   string
      Value Count = 12  object {Dictionary<string,object>}
      [0]   {[bar, Dictionary[String,Object]]}  KeyValuePair<string,object>
        Key "bar"   string
        Value   Count = 1   object {Dictionary<string,object>}
          [0]   {[baz, Dictionary[String,Object]]}  KeyValuePair<string,object>
          Key   "baz"   string
          Value Count = 3   object {Dictionary<string,object>}
            [0] {[3, title]}    KeyValuePair<string,object>
              Key   "3" string
              Value "title" object {string} 

这个KeyValuePair最终会是:"entity.foo.bar.baz.title.3", "3"

最有效的平展嵌套字典的方法

这只是一个简单的步行。递归实现应该是这样的:

static void Main( string[] args )
{
  Dictionary<string,object> nested = LoadNestedDictionary() ;
  Dictionary<string,string> flat   = new Dictionary<string, string>() ;
  Flatten(nested,flat) ;
  return;
}
/// <summary>
/// The wrapper method. Invoke this from your code
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
private static void Flatten( IEnumerable<KeyValuePair<string,object>> input , Dictionary<string,string> output )
{
  foreach ( KeyValuePair<string,object> item in input )
  {
    string key   = item.Key   ;
    object value = item.Value ;
    if ( value is string )
    {
      output.Add(key,(string)value) ;
    }
    else if ( value is Dictionary<string,object> )
    {
      Flatten( key , (IEnumerable<KeyValuePair<string,object>>) value , output ) ;
    }
    else
    {
      throw new InvalidOperationException();
    }
  }
  return ;
}
/// <summary>
/// The core method. Called only from the wrapper method
/// </summary>
/// <param name="root"></param>
/// <param name="input"></param>
/// <param name="output"></param>
private static void Flatten( string root , IEnumerable<KeyValuePair<string,object>> input , Dictionary<string,string> output )
{
  foreach ( KeyValuePair<string,object> item in input )
  {
    string segment = item.Key ;
    string key     = root + "." + segment ;
    object value   = item.Value ;
    if ( value is string )
    {
      string s = (string) value ;
      output.Add(key,s) ;
    }
    else if ( value is Dictionary<string,object> )
    {
      Dictionary<string,object> d = (Dictionary<string,object>) value ;
      Flatten(key,d,output);
    }
    else
    {
      throw new InvalidOperationException();
    }
  }
  return ;
}