.NET Dictionary、KeyValuePair 转换为 JSON 是怎样的格式?

作者:vkvi 来源:ITPOW(原创) 日期:2022-4-19

Dictionary

Dictionary<string, List<int>> list = new Dictionary<string, List<int>>();
list.Add("key1", new List<int>() { 1, 2, 3 });
list.Add("key2", new List<int>() { 1, 3, 4 });

如上,这么一段代码,利用 .NET 自带方法,转换成 JSON,是怎样的格式呢?

[
    {
        "Key": "key1",
        "Value": [
            1,
            2,
            3
        ]
    },
    {
        "Key": "key2",
        "Value": [
            1,
            3,
            4
        ]
    }
]

数组,每个数组中有 Key、Value 属性。注意是大写开头

KeyValuePair

KeyValuePair<string, int> kv = new KeyValuePair<string, int>("itpow", 1);

结果:

{
    "key": "itpow",
    "value": 1
}

注意是小写开头

相关阅读

相关文章