如何创建嵌套字典,下面在c#
中给出这是嵌套的json
{
"blue":[
{
"s":{
[
"store1",
"store2"
],
"availble"
}
},
{
"m":{
[
"store3",
"store4"
],
"not availble"
}
},
{
"l":{
[
"store5"
],
"availble"
}
}
]
},
{
"back":[
{
"xxl":{
[
"store2",
"store4"
],
"not availble"
}
},
{
"m":{
[
"store3",
"store4"
],
"not availble"
}
}
]
}
我尝试使用以下脚本,但它无效
var variations_hash = new Dictionary<string, List<string>>();
var stores = new[] {"s","m","xl","xxl","xxxl","v"};
var color_trans = new[] {"blue","black"};
foreach(var store in stores){
if (variations_hash.ContainsKey (color_trans)) {
List<String> list;
if (variations_hash.TryGetValue (color_trans, out list)) {
list.Add (store);
}
} else {
variations_hash[color_trans] = new List<string> { store };
}
}
我怎样才能在c#中创建嵌套字典?
我想要上面格式的嵌套字典
答案 0 :(得分:0)
这是一种生成嵌套字典的简单,优雅的方法
Dictionary<int, Dictionary<int, string>> dict = things
.GroupBy(thing => thing.Foo)
.ToDictionary(fooGroup => fooGroup.Key,
fooGroup => fooGroup.ToDictionary(thing => thing.Bar,
thing => thing.Baz));
如果有帮助,请告诉我,在我看来,这是生成嵌套字典的最有效方式。