When using the Deserialize method in JavascriptSerializer, what is required within your target class?
For example, I have a JSON string which contains people. Each person contains a name, age, and gender. If I only need to grab the name and age from this message, does my target class still NEED the gender part? Does deserialize know how to only grab the existing fields from the JSON string and leave others? Would the below work (note that there’s no gender in personinfo)?
List<person> mypeople = JavascriptSerializer.Deserialize <List<person>>(jsonstring);
class person
{
public List<personinfo> personinfo{ get; set; }
}
class personinfo
{
public string name { get; set; }
public int age { get; set; }
}
Deserialize() understands how to ignore parts of the string. You only need to specify the fields, which you require, in your target class.
I also found this question’s contents extremely helpful as I am new to C# – Using System.Web.Script.Serialization.JavascriptSerializer to deserialize JSON – how to?