3

I am porting a .Net framework built MVC application to .Net Core 3.1 and I have a case where System.Web.Helpers.Json.Encode() methods have been used.

As System.Web.Helpers is not available in .Net Core, any idea what alternate to use?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sachin
  • 459
  • 1
  • 7
  • 22

2 Answers2

3

You can use the built-in json serializer (System.Text.Json)

var obj = getMyObject();
var json = JsonSerializer.Serialize(obj);

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-core-3-1

taylorj94
  • 71
  • 3
0

You can use the following way:

  • .Net framework @Html.Raw(Json.Encode(item))
  • .Net 7: @Html.Raw(JsonSerializer.Serialize(item)) Or JsonConvert.SerializeObject(item)