How To: Serialize and Deserialize JSON with .NET Framework

Serializing an Object to JSON

To serialize an object to JSON you will need  to create a data contract, which is a class containing all the properties marked with attributes. To demonstrate this I will create a Person class and apply a DataContractAttribute and DataMemberAttribute.

[DataContract]
internal class Person
{
	[DataMember]
	internal string name;

	[DataMember]
	internal int age;
}

Now we need to write some code to populate the Person class with some data and then use DataContractJsonSerializer to serialize the object to Json and just for you information DataContractJsonSerializer has been available since .NET Framework 3.5. (more…)

Shares