How to serialize and deserialize JSON in .NET

Add the Newtonsoft.Json NuGet package:









And use it like so:

using Newtonsoft.Json;
...
// Serialize to JSON string
string jsonString = JsonConvert.SerializeObject(myObjectInstance);

// Deserialize from JSON string
var anotherObjectInstance = JsonConvert.DeserializeObject<MyObjectType>(jsonString);

Comments