I have a simple class like so:
public class MyClass
{
public string String1;
public string String2;
public MyClass()
{
}
public MyClass(string Json)
{
}
}
If the class is instantiated without a parameter, it just brings back an empty object.. However, when I pass a string of JSON to the constructor I would like to set all the properties equal to their value in the JSON.
I have done this outside of the class using JSON.NET by Newtonsoft, like this:
string JsonArray = Base64.Decode(HttpContext.Current.Request.QueryString["songinfo"]);
MyClass tmp = JsonConvert.DeserializeObject<MyClass>(JsonArray);
MyClass InstantiatedClass = tmp;
InstantiatedClass is now a fully populated object.. But, I would like to achieve this within the constructor itself, but I can't figure out a way to do it..
I've tried things like setting this = JsonConvert.DeserializeObject<MyClass>(Json)
, but of course, this
is read-only.
I know I could do it by parsing the Json and setting each variable equal to it's appropriate JSON attribute value, but I'd like to do it this way because it's cleaner.