In my app, I've got responses from a server. These responses can be :
{
"cmd":"read_ack",
"model":"sensor_ht",
"sid":"158d0001a2ddac",
"short_id":36192,
"data":"{\"voltage\":3005,\"temperature\":\"2741\",\"humidity\":\"5828\"}"
}
Or it can be :
{
"cmd":"read_ack",
"model":"magnet",
"sid":"158d000159febe",
"short_id":40805,
"data":"{\"voltage\":3045,\"status\":\"open\"}"
}
Can I use something like "dynamic" class ?
My class look like this :
public class RootObject
{
public string cmd { get; set; }
public string model { get; set; }
public string sid { get; set; }
public int short_id { get; set; }
public Data data { get; set; }
}
And my data class looks like :
public class Data
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int voltage { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string status { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string temperature { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string humidity { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string no_close { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string no_motion { get; set; }
}
Is it possible to have different classes for each type of response I get ? Is it possible to change the data type in RootObject class dynamically ?
Thank a lot