0

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?

LKon524
  • 31
  • 3

1 Answers1

1

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?

Vadym K
  • 1,647
  • 1
  • 10
  • 14