I have a JSON file that is being sent to an MVC controller via a POST request from the client side. Since there isn't a direct way to store JSON in C#, I'm trying to create models to store the data. Here is the structure of the JSON:
{
"SubscriptionsType1": {
"Obj1": {
"Value1": "3454234",
"Value2": "345643564",
"Value3": "665445",
"Value4": "True"
},
"Obj2": {
"Value1": "3454234",
"Value2": "345643564",
"Value3": "665445",
"Value4": "True"
},
"Obj3": {
"Value1": "3454234",
"Value2": "345643564",
"Value3": "665445",
"Value4": "True"
}
},
"SubscriptionsType2": {
"Obj1": {
"Value1": "3667635",
"Value2": "34563456",
"Value3": "234545",
"Value4": "False"
},
"Obj2": {
"Value1": "97865",
"Value2": "356356",
"Value3": "665757445",
"Value4": "True"
},
"Obj3": {
"Value1": "3454234",
"Value2": "345643564",
"Value3": "665445",
"Value4": "False"
}
},
//etc..
}
The models are the following:
public class RootObject {
public IEnumerable<SubscriptionObj> Subscriptions { get; set; }
}
public class Subscriptions {
IEnumerable<SubscriptionObj> objs;
}
public class SubscriptionObj
{
public Int64 Value1 {get;set;}
public Int64 Value2 {get;set;}
public Int64 Value3 {get;set;}
public Boolean Value4 {get;set;}
}
For some reason, if I create the model classes with this structure, the Root Object is still null after the it is passed the JSON object from the controller. However, when I explicitly create attributes to store each value of the JSON object, it works. Since the JSON object doesn't have a fixed number of elements, I need a way to dynamically store each attribute. How can this be done?