问题的关键在于如何将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"
你能提供代码吗?以上难以阅读 – 2013-03-27 15:50:47
为什么不把“entity.foo.bar.baz.title”作为密钥存储在一本字典中?如果这是不可能的,你可以提供一个查询例子,你将如何查询你的层次结构。 – duedl0r 2013-03-27 15:53:22
@ duedl0r它是嵌套的,因为密钥库非常大,并且以JSON的形式发送。嵌套大大减少了要传输的数据量。 – erodewald 2013-03-27 15:55:10