使用 c# 反序列化深度嵌套 Json 具有一个空白键

Deserialize deeply Nested Json having one blank key using c#

我正在尝试将下面嵌套的 Json 反序列化为自定义 c# 类型,这也在下面进行了描述,但我一直在反序列化对象中将路径键设置为 null。下面是代码,非常感谢任何关于如何正确反序列化的建议。

Json:

 {
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "API Service"
    },
    "host": "test@test.com",
    "basePath": "/test",
    "schemes": ["https"],
    "paths": {
        "/activity/actions": {
            "get": {
                "Tags": "activity",
                "Description": "Test",
                "    ": "GET/activity/actions"  //Empty Key

            },
            "put": {
                "Tags": "activity",
                "Description": "Test",
                "    ": "PUT/activity/actions"

            }
        },
        "/Test2Controller/DoAction": {
            "get": {
                "Tags": "Test2Controlle",
                "Description": "Test",
                "    ": "GET/activity/actions"

            },
            "put": {
                "Tags": "Test2Controlle",
                "Description": "Test",
                "    ": "PUT/activity/actions"

            },
            "POST": {
                "Tags": "Test2Controlle",
                "Description": "Test",
                "    ": "POST/activity/actions"

            }
        }
    }
}

我的 C# 对象

    public class SwaggerJsonObject
    {
        public string Swagger { get; set; }
        public Info Info { get; set; }
        public string Host { get; set; }
        public string BasePath { get; set; }
        public string[] Schemes { get; set; }
        public Path Paths { get; set; }
    }

    public class Info
    {
        public string Version { get; set; }
        public string Title { get; set; }
    }

    public class Path
    {
        public List<Controllers> Controllers { get; set; }
    }


    public class Controllers
    {
        public List<RestVerbs> RestVerbs { get; set; }
    }

    public class RestVerbs
    {
        public string[] tags { get; set; }
        public string description { get; set; }
        public string EmptyKey { get; set; }

    }

}

来电者:

var deserializedObjedt = JsonConvert.DeserializeObject<SwaggerJsonObject>(json);

Error/Issue:

Values in Paths key are null.

您可以将 paths 反序列化为 Dictionary<string, Dictionary<string, Dictionary<string,string>>>:

public class SwaggerJsonObject
{
    public string Swagger { get; set; }
    public Info Info { get; set; }
    public string Host { get; set; }
    public string BasePath { get; set; }
    public string[] Schemes { get; set; }
    public Dictionary<string, Dictionary<string, Dictionary<string,string>>> Paths { get; set; }
}

或者创建自定义 converter 来处理您的 class 结构。