Reputation: 31
I have some Json that I want to parse to a c# object that can come through like this:
{
"SomeObject" : {
"name" : "something",
"birthMonth" : "may"
}
}
Or Like this:
{
"SomeObject" : {
"name" : "something",
"birthMonth" : 5
}
}
Is there a way I can model my SomeObject class so that the birthMonth property can be a string or an integer?
Upvotes: 0
Views: 120
Reputation: 1665
Yes. You can use dynamic
keyword.
public class SomeObject {
public string name {get;set;}
public dynamic birthMonth {get;set;}
}
But better way probably is to use some json (framework dependent) technique to transform may
value to 5. For instance in newtonsoft.json
there is an variant is to use your own implementation of JsonConverter
.
Here is an example: How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?
Upvotes: 1