c# - 以任何形式(Json、xml 等)序列化 Dictionary<String、Dictionary<String、String>>

标签 c# windows windows-8

将Dictionary>序列化到可以存储的程度,之后反序列化。 我进行了大量谷歌搜索,但找不到与 .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);

关于c# - 以任何形式(Json、xml 等)序列化 Dictionary<String、Dictionary<String、String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13153767/

相关文章:

c# - 如何在 Visual Studio 中创建插件以从设置文件生成 javascript、CSS 和其他代码

c# - 显示在引用程序集中运行的任务的进度

c++ - 如何在基于 Windows 8 Xaml 的 ListView 中禁用缓存?

windows-8 - 使用 Google Drive API javascript 上传或更新文件时标题为 'Untitled'?

python 安装工具 : ImportError: cannot import name Library

c# - 使用 C# 的 Windows 8 Live Tile 后台代理

c# - 批量插入记录到 SQL Server 数据库

c# - LINQ - 从所有 parent 那里获取所有子记录

windows - Windows 上/usr/lib/的等价物是什么?

获取电子邮件哈希时出现 Windows Live Contacts API 错误 - JavaScript