2

关键是构建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"

4

1 回答 1

1

这只是一个简单的treewalk。递归实现应如下所示:

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 ;
}
于 2013-03-27T17:32:21.877 回答