I have some Json that I want to parse to ac# 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?
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.