【问题标题】:C# Serialize JSON to simple Dictionary<string,string>C# 将 JSON 序列化为简单的 Dictionary<string,string>
【发布时间】:2020-08-19 12:22:35
【问题描述】:

我有以下 json:

{
    "Name" : "Value1",
    "Data": [
        {
            "UserName": "test",
            "email": "test@test.com",
            "UserInformation": {
                "City": "city",
                "country:": "country"
            }
        },        
        {
            "UserName": "test1",
            "email": "test1@test.com"
        }
    ]
}

我想要下一本字典:

{
    "Name" : "Value1",
    "Data[0].UserName": "test",
    "Data[0].email": "test@test.com",
    "Data[0].UserInformation.City": "city",
    "Data[0].UserInformation.country": "country",
    "Data[1].UserName": "test1",
    "Data[1].email": "test1@test.com"
}

使用 Newtonsoft.Json 无助于解决我的问题。拜托,你能帮帮我吗?

【问题讨论】:

  • 这是一个奇怪的要求。你见过json2csharp吗?该站点可以将您的 JSON 转换为 C# 类,您可以使用这些类来反序列化您的 JSON。

标签: c# json serialization


【解决方案1】:

如果我理解正确,您正在寻找这样的东西(使用 Newtonsoft.Json):

var dictionary = JObject.Parse(json)
    .Descendants()
    .OfType<JValue>()
    .ToDictionary(jv => jv.Path, jv => jv.ToString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多