Using C# and specifically JSON.net how to do I deal with JSON object that can have a dynamic value. For example:
{ "message_id":"123-456", "data":["data","list"], "type":"MSG_TYPE1" }
{ "message_id":"123-457", "data":"my data string", "type":"MSG_TYPE2" }
{ "message_id":"123-458", "data":{"key1":"value1","key2":"value2"}, "type":"MSG_TYPE3" }
The "data" value can be any type. In C#, I defined a ServiceMessage class that contains these properties but what type of property should "data" be. I was looking at a JToken or JContainer but I'm not sure the best way to implement.
public class ServiceMessage
{
public string message_id { get; set; }
public JContainer data { get; set; }
public string type { get; set; }
public string getJSON()
{
string json = JsonConvert.SerializeObject(this);
return json;
}
public void setJSON(string json)
{
dynamic jsonObj = JsonConvert.DeserializeObject(json);
this.message_id = jsonObj.message_id;
this.type = jsonObj.type;
this.data = // what goes here.
}
}