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