0

I am trying to deserialize below nested Json into a custom c# type which is also described below but I keep getting the paths key as null in the deserialized object. Below is the code, any suggestions on how can this be correctly deserialized are really appreciated.

Json:

 { "swagger": "2.0", "info": { "version": "v1", "title": "API Service" }, "host": "[email protected]", "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" } } } } 

My C# Object

 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; } } } 

Caller:

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

Error/Issue:

Values in Paths key are null. 
4
  • Your c# class variables must match case of your json. Or, you can use jsonProperty. Not sure how controller is going to work. You will probably need to use JObject.Parse method and iterate over the properties Commented Jun 25, 2020 at 23:47
  • The other problem I see at the moment is that your C# property Paths doesn't match the JSON. I'd expect the JSON to look like "paths": { "controllers": [ { "restVerbs": [ "description": "abc" ] } ] } based on your C# classes. Note that we don't see controllers or restVerbs in the JSON in your question. Commented Jun 25, 2020 at 23:50
  • /activity/actions & /Test2Controller/DoAction are dynamically generated keys, so, I can't hard code their KeyNames thats why I created a controller type. I was hoping there was a way to deal with these dynamic keys in Json. Commented Jun 25, 2020 at 23:53
  • Your json doesnt show paths being a list either. Commented Jun 26, 2020 at 0:09

1 Answer 1

1

You can deserialize paths as 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; } } 

Or create custom converter to handle your class structure.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.