将MVC模型序列化为视图中的JSON-未捕获的语法错误:意外的令牌i

将MVC模型序列化为视图中的JSON-未捕获的语法错误:意外的令牌i,json,asp.net-mvc-4,Json,Asp.net Mvc 4,我使用JQWidgets,我试图在使用JSON填充JQWidgets表之前将模型序列化为JSON。但是我得到了这个错误: Uncaught SyntaxError: Unexpected token i 我认为我的JSON是无效的,因为我是如何通过混合RAZOR和javaScript/jQuery来序列化我的模型对象的。这是我代码的其余部分: public ActionResult\u NewsWidget() { var v=JArray.Parse(JsonConvert.Serial

我使用JQWidgets,我试图在使用JSON填充JQWidgets表之前将模型序列化为JSON。但是我得到了这个错误:

Uncaught SyntaxError: Unexpected token i 
我认为我的JSON是无效的,因为我是如何通过混合RAZOR和javaScript/jQuery来序列化我的模型对象的。这是我代码的其余部分:


public ActionResult\u NewsWidget()
{
var v=JArray.Parse(JsonConvert.SerializeObject(news.GetNewsItems(),格式:formatting.Indented));
var模型=(从p到v)
选择new ModelNewsWidget()
{
Id=p[“唯一Id”].ToString(),
ImagePath=“”,
正文=
“+p[“Title”]+”

”+ “+p[“AddedOn”].ToString().子字符串(0,10)+”
”+ p[“BodyTextFormatted”].ToString().子字符串(0,18)+“

”+ p[“BodyText”].ToString()子字符串(18100).插入(100,“…”) }).ToList(); ViewBag.StartupScript=“initNewsWidget();”; 返回视图(“~/PartialViews/News/_NewsWidget.cshtml”,model); }
局部视图

@model List<AMT2014_Prototype.Models.News.ModelNewsWidget>

<script>

    function _createWindow() {
        $("#jqxwindow-news").jqxWindow({ width: 720, height: 600, autoOpen: false, draggable: true });
    };

    function bindNewsTable(data) {
        var source =
        {
            dataType: "json",
            dataFields: [
                { name: 'id', type: 'int' },
                { name: 'imagePath', type: 'string' },
                { name: 'title', type: 'string' },
                { name: 'text', type: 'string' }
            ],
            localData: data,
        };
        var dataAdapter = new $.jqx.dataAdapter(source);

        $("#newsTable").jqxDataTable(
        {
            source: dataAdapter,
            showHeader: false,
            autoRowHeight: false,
            width: '100%',
            columns: [
                { text: 'Image', dataField: 'imagePath', width: 120 },
                { text: 'Body Text', dataField: 'text', width: 320 }
            ]
        });
    }

function initNewsWidget() {
    var json = '@Html.Raw(Json.Encode(Model))';
    console.log(json);
    bindNewsTable(json);
}
@model List”,
“BodyText:”树真菌ID:Inonotus dryadeus

16/09/2013
参考号:204201/252874

子实体是一种名为干褐孔菌的真菌的子实体。橡树是最常见的 } ]ick=“initSingleNews(6)”>更多信息 } ] 结果 第4行的分析错误: …路径“:”
  • 我应该从控制器返回JSON吗?我以前在编码方面有问题,这就是为什么我试图在我的视图中编码我的模型
  • 考虑到这一点,这样做很好,但仍然不是一种令人信服的方式,它仍然会起作用

  • 为什么我的JSON在Lint中无效
  • 这是因为您在json数据的ImagePath和BodyText属性中生成的超链接有多个双引号(“),属性值应该是单引号(')

    我的建议是使用Json序列化程序(使用Newtonsoft)将模型序列化为Json格式

    var json = '@Html.Raw(JsonConvert.SerializeObject(model))';
    

    命名空间:
    使用Newtonsoft.Json;
    您的
    Json
    无效:

    [
        {
            "Id": "6",
            "ImagePath": "<a onclick="initSingleNews(6)" href="javasscript: void(0)"><img style="width: 100px;" alt="" src="9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg"></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class="newsMore" href="#" onclick="initSingleNews(6)" >More</a>"
        }
    ]
    
    [
    {
    “Id”:“6”,
    “图像路径”:“,
    “BodyText:“树木真菌ID:Inonotus dryadeus

    2013年9月16日
    参考:204201/252874

    子实体是一种名为Inonotus dryadeus的真菌。橡木是最常见的…” } ]
    它应该是这样的:

    [
        {
            "Id": "6",
            "ImagePath": "<a onclick=\"initSingleNews(6)\" href=\"javasscript: void(0)\"><img style=\"width: 100px;\" alt=\"\" src=\"9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg\"></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class=\"newsMore\" href=\"#\" onclick=\"initSingleNews(6)\" >More</a>"
        }
    ]
    
    [
    {
    “Id”:“6”,
    “图像路径”:“,
    “BodyText:“树木真菌ID:Inonotus dryadeus

    2013年9月16日
    参考:204201/252874

    子实体是一种名为Inonotus dryadeus的真菌。橡木是最常见的…” } ]
    所有双引号都应该转义。这可以通过

    大概是这样的:


    var json=@Html.Raw((新JavaScriptSerializer()).Serialize(Model));
    您的json字符串无效

    试试这个

    [
        {
            "Id": "6",
            "ImagePath": "<a onclick='initSingleNews(6)' href='javasscript: void(0)'><img style='width: 100px;' alt='' src='9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg'></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class='newsMore' href='#' onclick='initSingleNews(6)' >More</a>"
        }
    ]
    
    [
    {
    “Id”:“6”,
    “图像路径”:“,
    “BodyText:“树木真菌ID:Inonotus dryadeus

    2013年9月16日
    参考:204201/252874

    子实体是一种名为Inonotus dryadeus的真菌。橡木是最常见的…” } ]
    我尝试使用Newtonsoft,当我调用bindNewsTable(json)时;我收到一个关于未捕获语法的错误错误:意外标记iCan能否显示生成的json数据以及bindNewsTable函数的作用?验证json首先尝试此链接以验证json是否有效not@DotNetIsMyPower我使用了你的链接,它抱怨在JSON中使用冒号。我最初是序列化我的我的控制器中有JSON,但编码给我带来了问题。这意味着你的JSON无效,你能在这里发布你的JSON字符串吗?
    [
        {
            "Id": "6",
            "ImagePath": "<a onclick="initSingleNews(6)" href="javasscript: void(0)"><img style="width: 100px;" alt="" src="9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg"></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class="newsMore" href="#" onclick="initSingleNews(6)" >More</a>"
        }
    ]
    
    [
        {
            "Id": "6",
            "ImagePath": "<a onclick=\"initSingleNews(6)\" href=\"javasscript: void(0)\"><img style=\"width: 100px;\" alt=\"\" src=\"9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg\"></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class=\"newsMore\" href=\"#\" onclick=\"initSingleNews(6)\" >More</a>"
        }
    ]
    
    [
        {
            "Id": "6",
            "ImagePath": "<a onclick='initSingleNews(6)' href='javasscript: void(0)'><img style='width: 100px;' alt='' src='9750cf57-bc14-4205-9e6c-83c8406f3f82.jpg'></img></a>",
            "BodyText": "<b>Tree fungus ID: Inonotus dryadeus</b></br></br><b>16/09/2013</b></br>Ref: 204201/252874</br></br>The fruiting bodies are those of a fungus called Inonotus dryadeus. Oak is the most common host of...<a class='newsMore' href='#' onclick='initSingleNews(6)' >More</a>"
        }
    ]