我正在尝试将下面的嵌套 Json 反序列化为自定义 c# 类型,该类型也在下面描述,但我在反序列化对象中不断将路径键作为 null。下面是代码,非常感谢有关如何正确反序列化的任何建议。
杰森:
{
"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);
错误/问题:
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; }
}
或者创建自定义转换器来处理您的类结构。