JavaScriptSerializer is not allowed in .net core project?

Nivitha Gopalakrishnan picture Nivitha Gopalakrishnan · Jan 9, 2019 · Viewed 19.3k times · Source

I am working in .net core project. I want to serialize the objects using JavaScriptSerializer.

JavaScriptSerializer Serializer = new JavaScriptSerializer();

I added the namespace using System.Web.Script.Serialization;. It throws errors. I googled it. I saw like .net core project does not have System.Web.Script.Serialization; in some sites.

Is there have any another way to serialize the objects in the .net core like JavaScriptSerializer?

Answer

Michael Ceber picture Michael Ceber · Jan 9, 2019

In .net core the most common way of serializing and deserializing objects (JSON) using Newtonsoft.Json.

You need to install the Nuget Package of Newtonsoft.Json

add using a statement like:

using Newtonsoft.Json;

and use it as:

object o = JsonConvert.DeserializeObject(json1);
string json2 = JsonConvert.SerializeObject(o, Formatting.Indented);