The serialization using Newtonsoft.Json creates a json text succesfully:
@model IEnumerable<CardGameApp.RootObject>
@{
ViewData["Title"] = "Game";
Layout = "~/views/Shared/_Layout.cshtml";
List<CardGameApp.RootObject> deck1 = Model.Take(40).ToList();
List<CardGameApp.RootObject> deck2 = Model.Skip(40).ToList();
var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(deck1);
var json2 = Newtonsoft.Json.JsonConvert.SerializeObject(deck2);
}
Whereas the 2nd task throws an error like:
Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()
var _deck1 = JSON.parse(@Html.Raw(json1));
var _deck2 = JSON.parse(@Html.Raw(json2));
And if i put '' inside the parenthesis like this:
var _deck1 = JSON.parse('@Html.Raw(json1)');
var _deck2 = JSON.parse('@Html.Raw(json2)');
It throws an error like:
Uncaught SyntaxError: missing ) after argument list.
Any tip will be helpfull!
Since JSON is valid JavaScript code, all you have to do in the JavaScript is:
var _deck1 = @Html.Raw(json1);
var _deck2 = @Html.Raw(json2);
Basically, the JSON becomes a JavaScript object literal in your script tag.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.