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?
Because of the varying key in the root object and subscriptions, you should use a dictionary.
The above would serialize to the JSON in the OP but you loose the strongly typed keys/property names
The json corresponding to your c# classes should use arrays, something like
The c# classes corresponding to your json with no arrays will look something like:
From your JSON file, we can extract the model classes below:
If the list of subscriptions is dynamic, then you should change your JSON file to look like the one below:
for that datastructure i would deserialize the object to dynamic. then you can check if there is "SubscriptionsType1" or "SubscriptionsType2", ...
with the dynamic i would create the model classes and fill them in a self brewed deserialization method/class.
the design of the model is not serialization friendly, can you change the json that you will get by changing the contract with the caller?
if so here is my suggestion:
your model would change to:
is that what you are trying to get?