以任何forms(Json,xml等)序列化Dictionary <String,Dictionary <String,String>

将字典<string,字典<string,string>序列化到它可以被存储的位置,然后被序列化。 我做了相当多的search,并找不到与.NET 4.5完全兼容的类或函数。

开箱即用的JavaScriptSerializer应该能够处理你的字典。 它也应该给出相当紧凑的输出。

 var source = new Dictionary<string, Dictionary<string, string>> { { "one", new Dictionary<string, string> { { "a", "1a"}, { "b", "1b" } } }, { "two", new Dictionary<string, string> { { "a", "2a"}, { "b", "2b" } } } }; var serializer = new JavaScriptSerializer(); // This gives {"one":{"a":"1a","b":"1b"},"two":{"a":"2a","b":"2b"}} string serialized = serializer.Serialize(source); // This gives a clone of the original dictionary. var deserialized = serializer.Deserialize <Dictionary<string, Dictionary<string, string>>>(serialized); 

那么我发现解决方案是使用JSON.NET

 public String Serialize(Dictionary<int, Dictionary<String, String>> all) { String abc = JsonConvert.SerializeObject(all, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple }); return abc; } public Dictionary<int, Dictionary<String, String>> DeSerialize(String text) { Dictionary<int, Dictionary<String, String>> abc; abc = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<String, String>>>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple, }); return abc; } 

你可以看看SerializableDictionary

你应该使用下面,它会让你的生活更加容易。

StorageHelper

或使用

WinRT XAML工具包