0

I use:

@Html.Raw(Json.Encode(Model.Events))

to display model in JSON format.

On server side I have model:

{"Title":"Party","Url":"site.com/events?id=1&view=table"}

But after on client side I got JSON:

{"Title":"Party","Url":"site.com/events?id=1\u0026view=table"}

How can I display JSON without converting of special symbols ?

Kirill
  • 429
  • 1
  • 6
  • 18
  • Why are you using [Json.Encode](https://msdn.microsoft.com/en-us/library/system.web.helpers(v=vs.111).aspx) ? Almost all projects use JSON.NET, even Web API. – Panagiotis Kanavos Feb 06 '18 at 13:35
  • 1
    ASP.NET and .NET in general doesn't need anything special to work with Unicode - all strings are already Unicode. The output of ASP.NET pages is UTF8 by default. SO uses ASP.NET and doesn't perform anything special to display strings, code, URLs. It's the deprecated `Json.Encode` method that causes the problem – Panagiotis Kanavos Feb 06 '18 at 13:41

1 Answers1

0

FYI, u0026 is the unicode encoded value for ampersand & character.

Here are two solutions to parse Json correctly.

1. Install Json.Net package in your project.

This is the easiest and most used method.

//In your view add

@using Newtonsoft.Json;

@Html.Raw(JsonConvert.SerializeObject(Model))

2. Use Regex Unescape Method

@{ 
   string str = @Html.Raw(Json.Encode(Model)).ToString();
}

@{ @System.Text.RegularExpressions.Regex.Unescape(str) }

References:

1.

2.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196