JavaScriptSerializer is not allowed in .net core project?

For ASP.NET Core 3.1 we can use JsonSerializer.Deserialize from System.Text.Json with a string/dynamic dictionary Dictionary<string, dynamic>.

Example loading ClientApp\package.json:

using System;
using System.Text.Json;
using System.Collections.Generic;

...

var packageJson = File.ReadAllText(Path.Combine(env.ContentRootPath, "ClientApp", "package.json"));

var packageInfo = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(packageJson);

Console.WriteLine("SPA Name: {0}, SPA Version: {1}", 
    packageInfo["name"], packageInfo["version"]);

Of course we'll have to copy ClientApp\package.json to the publish directory using a Target > Copy command inside .csproj.


Install Newtonsoft.Json and then

in Controller:

string data = JsonConvert.SerializeObject(new { MyUser = user, MyId = id });
object dataObj = JsonConvert.DeserializeObject(data);
return Json(dataObj);

and in Javascript file:

$.ajax({
   // some codes!
   success: function(data){
      var user = data.MyUser;
      var id = data.MyId
   }
});

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);