Header Ads

JSON serialising/deserialising in dotnet

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 = "Navas", LastName = "Khan", Age = 27 };

string json = JsonConvert.SerializeObject(inputPerson);

The json string variable now contains the value:


{"FirstName":"Navas","LastName":"Khan","Age":27}

Reconstructing the object back from a JSON string is as simple as:


var outputPerson = JsonConvert.DeserializeObject(json);

1 comment:

  1. Peculiar article, just what I was looking for.



    Also visit my web blog :: Reviews on Burn The Fat Feed The
    Muscle ()

    ReplyDelete



Powered by Blogger.