Jquery/.NET Core JSON returned cannot be parsed

问题: This is what I send to my controller: "[{"PropertyName":"fewfewf","Key":"ewgwewe","PrimaryValue":"","SecondaryValue":"","TertiaryValue":"","MinimumValue":"","MaximumV...


This is what I send to my controller:


This is what I receive back from the server after I parse and serialize it again.

enter image description here I am very confused as I haven't changed anything. When I try to parse it Jquery side, I get this error:

Uncaught SyntaxError: Unexpected token & in JSON at position 2

This is the jquery code

if ("@Model.FormPropertiesList".length !== 0) {

Am i Missing something?

EDIT: This is my serverside code

var list = JsonConvert.DeserializeObject<List<NewFormPropertyViewModel>>(Input.FormPropertiesList);
Input.FormPropertiesList = JsonConvert.SerializeObject(list);
return View("Create", Input);

This is how i convert the string to JSON

$("#@Html.IdFor(m => m.FormPropertiesList)").val(JSON.stringify(dataSet));


The string is being HTML encoded. You can use @HTML.Raw to tell razor to put the string as is. One of the reasons strings get encoded is for security purposes. Say you are trying to display some data you just fetched from a database and that data has some "malicious" JavaScript, while this code will be harmless to the database, it will affect the client once the view is rendered. You can try:

if ("@Model.FormPropertiesList".length !== 0) {


First replace your "&quot"

var serverrep= "[{&quot;PropertyName&quot;:&quot;fewfewf&quot;,&quot;Key&quot;:&quot;ewgwewe&quot;,&quot;PrimaryValue&quot;:&quot;&quot;,&quot;SecondaryValue&quot;:&quot;&quot;,&quot;TertiaryValue&quot;:&quot;&quot;,&quot;MinimumValue&quot;:&quot;&quot;,&quot;MaximumValue&quot;:&quot;&quot;}]"

var serverRepWithoutQuots=serverrep.replace(/&quot;/g, '"');


I had an issue like this when passing JSON to my template then to my js file in Django. When you do this in Django you pass something from the view in the context, then when you're in the Django template you would do something like

var someName = {{ fromView }};

and I had it wrapped in doubles quotes

var someName = "{{ fromView }}";

so it was escaping the double quotes in the JSON that was parsed. I spent like 2 hours trying to figure out what was up and I literally had to just change the double quotes to single quotes because it was reading it as a string with double quotes that had another string(json string) with double quotes in it

var someName = '{{ fromView }}';

point being I would check the JSON to make sure you're not passing it the wrong way and it's being encoded improperly / adding extra quotes somewhere

  • 发表于 2019-03-17 06:52
  • 阅读 ( 126 )
  • 分类:sof


请先 登录 后评论


作家榜 »

  1. 小编 文章