I’m receiving JSON, that i need to Deserialize. I’m using JavaScriptSerializer to do so.
E.g. Objects
class Player
{
public string name{ set; get; }
public float yPos { set; get; }
public float xPos { set; get; }
}
class Communication
{
public string id{ set; get; }
public string message{ set; get; }
public string status{ set; get; }
}
E.g. JSONs:
var json1 = "[{\"name\":\"Master\",\"xPos\":\"34.67\",\"yPos\":\"85.36\"}, {\"name\":\"Puppet\",\"xPos\":\"19.56\",\"yPos\":\"75.19\"}]";
var json2 = "[{\"id\":\"5697862\",\"message\":\"Hello\",\"status\":\"85.36\"}, {\"id\":\"4698458\",\"message\":\"Hi\",\"status\":\"75.19\"}]";
Deserializer Method:
private static List<T> Deserialize<T>(string json)
{
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
List<T> obj = s.Deserialize<List<T>>(json);
return obj;
}
But here is the problem I have two different kind of JSON messages coming. So how do I figure out to what object I need to Deserialize?
Instead of deserializing to a specific class you can process the result dynamically