Imagine we have a simple class that models a person:
internal class PersonWe can create an instance of this class and then serialize the object into a JSON string using the JsonConvert class as follows:
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
var inputPerson = new Person()The json string variable now contains the value:
{ FirstName = "Alan", LastName = "Turing", Age = 41 };
string json = JsonConvert.SerializeObject(inputPerson);
{"FirstName":"Alan","LastName":"Turing","Age":41}
Reconstructing the object back from a JSON string is as simple as:
var outputPerson = JsonConvert.DeserializeObject<Person>(json);