How to use transfer JSON data from MVC\VIEW to JavaScript JSON data

Maryan 21 Reputation points
2021-11-03T22:25:41.847+00:00

I serialized my data to JSON from the controller:
// Serialization
string jsonActivities = JsonSerializer.Serialize(ViewUserNotifications);
ModelData.JsonNotifications = jsonActivities;

View:
@默 .JsonNotifications

[{"Id":7,"UserSender":10,"UserReciever":4,"UserSenderName":"Maya","UserSenderSurname":"Robertson","UserSenderProfileImage":"88cf1027-eafd-493f-8b9c-add3f6812eb0_64.jpg","Message":"Maya is following you","Seen":true,"Created":"2021-09-04T21:07:50.3294555","Status":3}]

I wonder how can I use this Json data with javascript?I tried like this:

JS:
var data = JSON.parse(@默 .JsonNotifications);
console.log(data);

However, in the console I got an error:

Uncaught SyntaxError: expected property name, got '&'

Any help is appreciated!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,331 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
744 questions
{count} votes

1 answer

Sort by: Oldest
  1. AgaveJoe 20,301 Reputation points
    2021-11-04T10:36:58.147+00:00

    The view automatically encodes string content to protect you from nasty JavaScript injection. You must opt-in if you trust the string content.

    var data =  @Html.Raw(Model.JsonNotifications);
    console.log(data);