{
"results": [
{
"content1": {
"prop1" : "value1",
"prop2" : "value2"
},
"content2": {
"prop1" : "value1",
"prop2" : "value2"
},
"contentn": {
"prop1" : "value1",
"prop2" : "value2"
}
}
]
}
I am having trouble converting this json to a dynamic C# object, the case here is that ‘content1’ to ‘content’ will always be different and dynamic content and I do need to convert it to something like a dictionary.
The idea is to reduce the classes as, I will need to create tons of them.
PS: I am using a 3rd party api that’s returning the data as above.
Tried numerous ways to handle this.
JsonConvert
to try to map to Dictionary
, List
and the same for JsonSerializer.Deserialize
4
Answers
The solution below uses System.Text.Json
Create a
Content
class:and now you can deserialize:
You can try with the ExpandoObject very powerfull for that kind of jobs.
you can try this code
class
try this::