1

将Dictionary<String, Dictionary<String, String>> 序列化到可以存储的地步,稍后反序列化。我进行了相当多的谷歌搜索,但找不到与 .NET 4.5 完全兼容的类或函数。

4

4 回答 4

2

开箱即用的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);
于 2012-10-31T08:38:48.407 回答
1

好吧,我发现解决方案是使用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;
         }
于 2012-10-31T17:02:03.793 回答
0

你可以看看SerializableDictionary

于 2012-10-31T08:37:21.693 回答
0

你应该使用以下,它会让你的生活更轻松。

存储助手

或使用

WinRT XAML 工具包

于 2012-10-31T10:00:45.840 回答