为什么某些类实例显然没有正确转换为json?

时间:2013-09-19 15:43:06

标签: jquery asp.net json html5 razor

我通过在razor / cshtml文件上调用getJSON来生成页面上div内容的内容。 90%的代码工作 - 从2013年到1960年 - 正如你在http://www.awardwinnersonly.com/看到的那样,从书籍下拉列表中选择“Hugos(科幻小说)”,但我添加的最后几个“记录”中有些东西(来自1959年至1946年)显然导致对getJSON('getHugos.cshtml')的调用失败,因为那些额外的记录根本没有显示页面。

(问题“记录”暂时被注释掉)

注意:带有时髦价值的“记录”,例如“blankThis”和“blankThat”,以及类别设置为一年的情况不是问题;它们只是相当kludgy - 如果类别长度为四个字符,“记录”是一年,并且处理方式不同。

此外,以“ - ”作为其值的元素不是问题 - 在这些情况下,没有创建相应的按钮(对于Kindle,Hardcopy或平装版)。

以下是cshtml文件中代码的子集。省略号点表示许多省略的“记录”;其中大部分,显然是问题所在,是在第二组省略号和评论之后:

@{
    var books = new List<BookClass>
    {
        new BookClass{Year=2013, YearDisplay="blankYearDisplay", Category="2013", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",   
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
. . .
new BookClass{Year=2001, YearDisplay="2001", Category="Best Novella", Title="The Ultimate Earth", Author="Jack Williamson", KindleASIN="B00DV8TSHO", HardboundASIN="--", PaperbackASIN="1612421547",   
ImgSrc="http:images.amazon.com/images/P/B00DV8TSHO.01.MZZZZZZZ"}
. . . 
// the above works, from 2013 down to 1960, but something in the last few "records" is apparently causing it to fail
        new BookClass{Year=1959, YearDisplay="blankYearDisplay", Category="1959", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1959, YearDisplay="1959", Category="Best Novel", Title="A Case of Conscience", Author="James Blish", KindleASIN="--", HardboundASIN="B000J52BAI", PaperbackASIN="0345438353",     
ImgSrc="http:images.amazon.com/images/P/0345438353.01.MZZZZZZZ"},
        new BookClass{Year=1958, YearDisplay="blankYearDisplay", Category="1958", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1958, YearDisplay="1958", Category="Best Novel", Title="The Big Time", Author="Fritz Leiber", KindleASIN="B004UJHII4", HardboundASIN="0899685374", PaperbackASIN="B003YMNGGG",     
ImgSrc="http:images.amazon.com/images/P/B004UJHII4.01.MZZZZZZZ"},
        new BookClass{Year=1956, YearDisplay="blankYearDisplay", Category="1956", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1956, YearDisplay="1956", Category="Best Novel", Title="Double Star", Author="Robert A. Heinlein", KindleASIN="B0050OVMWG", HardboundASIN="0839824467", PaperbackASIN="0345330137",     
ImgSrc="http:images.amazon.com/images/P/B0050OVMWG.01.MZZZZZZZ"},
        new BookClass{Year=1955, YearDisplay="blankYearDisplay", Category="1955", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1955, YearDisplay="1955", Category="Best Novel", Title="They'd Rather Be Right (also known as The Forever Machine)", Author="Mark Clifton and Frank Riley", KindleASIN="--", HardboundASIN="--",     
PaperbackASIN="0881848425", ImgSrc="http:images.amazon.com/images/P/0881848425.01.MZZZZZZZ"},
        new BookClass{Year=1954, YearDisplay="blankYearDisplay", Category="1954", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1954, YearDisplay="1954", Category="Best Novella", Title="A Case of Conscience", Author="James Blish", KindleASIN="--", HardboundASIN="B000M0BM5A", PaperbackASIN="B005KEM8TW",     
ImgSrc="http:images.amazon.com/images/P/B005KEM8TW.01.MZZZZZZZ"},
        new BookClass{Year=1953, YearDisplay="blankYearDisplay", Category="1953", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1953, YearDisplay="1953", Category="Best Novel", Title="The Demolished Man", Author="Alfred Bester", KindleASIN="B00D2ITJLS", HardboundASIN="B000UF0KTQ", PaperbackASIN="0679767819",     
ImgSrc="http:images.amazon.com/images/P/B00D2ITJLS.01.MZZZZZZZ"},
        new BookClass{Year=1951, YearDisplay="blankYearDisplay", Category="1951", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1951, YearDisplay="1951", Category="Best Novella", Title="The Man Who Sold the Moon", Author="Robert A. Heinlein", KindleASIN="B00ELJZZ24", HardboundASIN="--", PaperbackASIN="1451639228",     
ImgSrc="http:images.amazon.com/images/P/B00ELJZZ24.01.MZZZZZZZ"},
        new BookClass{Year=1946, YearDisplay="blankYearDisplay", Category="1946", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN",     
PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
        new BookClass{Year=1946, YearDisplay="1946", Category="Best Novella", Title="Animal Farm", Author="George Orwell", KindleASIN="B003ZX868W", HardboundASIN="0151010269", PaperbackASIN="184046254X",     
ImgSrc="http:images.amazon.com/images/P/B003ZX868W.01.MZZZZZZZ"}
    };
    Response.ContentType = "application/json";
    Json.Write(books, Response.Output);
}

当然,诸如“他们相当正确”(也称为永远机器)之类的字符串不是问题,是吗?显然,它编译并运行......

如果可能,我会尝试单步执行getHugos.cshtml;另外,在浏览器控制台中查看是否有任何错误的消息,但有没有人知道有关cshtml文件的getJSON变幻无常的事情可能会对这个难题有所了解?

更新

以这种方式重新排列并重新格式化:

var books = new List<BookClass> {
    new BookClass{Year=1959, YearDisplay="blankYearDisplay", Category="1959", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1958, YearDisplay="blankYearDisplay", Category="1958", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1956, YearDisplay="blankYearDisplay", Category="1956", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1955, YearDisplay="blankYearDisplay", Category="1955", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1954, YearDisplay="blankYearDisplay", Category="1954", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1953, YearDisplay="blankYearDisplay", Category="1953", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1951, YearDisplay="blankYearDisplay", Category="1951", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},
    new BookClass{Year=1946, YearDisplay="blankYearDisplay", Category="1946", Title="blankTitle", Author="blankAuthor", KindleASIN="blankKindleASIN", HardboundASIN="blankHardboundASIN", PaperbackASIN="blankPaperbackASIN", ImgSrc="blankImgSrc"},

    new BookClass{Year=1959, YearDisplay="1959", Category="Best Novel",   Title="A Case of Conscience",                                       Author="James Blish",                  KindleASIN="--",         HardboundASIN="B000J52BAI", PaperbackASIN="0345438353", ImgSrc="http:images.amazon.com/images/P/0345438353.01.MZZZZZZZ"},
    new BookClass{Year=1958, YearDisplay="1958", Category="Best Novel",   Title="The Big Time",                                               Author="Fritz Leiber",                 KindleASIN="B004UJHII4", HardboundASIN="0899685374", PaperbackASIN="B003YMNGGG", ImgSrc="http:images.amazon.com/images/P/B004UJHII4.01.MZZZZZZZ"},
    new BookClass{Year=1956, YearDisplay="1956", Category="Best Novel",   Title="Double Star",                                                Author="Robert A. Heinlein",           KindleASIN="B0050OVMWG", HardboundASIN="0839824467", PaperbackASIN="0345330137", ImgSrc="http:images.amazon.com/images/P/B0050OVMWG.01.MZZZZZZZ"},
    new BookClass{Year=1955, YearDisplay="1955", Category="Best Novel",   Title="They'd Rather Be Right (also known as The Forever Machine)", Author="Mark Clifton and Frank Riley", KindleASIN="--",         HardboundASIN="--",         PaperbackASIN="0881848425", ImgSrc="http:images.amazon.com/images/P/0881848425.01.MZZZZZZZ"},
    new BookClass{Year=1954, YearDisplay="1954", Category="Best Novella", Title="A Case of Conscience",                                       Author="James Blish",                  KindleASIN="--",         HardboundASIN="B000M0BM5A", PaperbackASIN="B005KEM8TW", ImgSrc="http:images.amazon.com/images/P/B005KEM8TW.01.MZZZZZZZ"},
    new BookClass{Year=1953, YearDisplay="1953", Category="Best Novel",   Title="The Demolished Man",                                         Author="Alfred Bester",                KindleASIN="B00D2ITJLS", HardboundASIN="B000UF0KTQ", PaperbackASIN="0679767819", ImgSrc="http:images.amazon.com/images/P/B00D2ITJLS.01.MZZZZZZZ"},
    new BookClass{Year=1951, YearDisplay="1951", Category="Best Novella", Title="The Man Who Sold the Moon",                                  Author="Robert A. Heinlein",           KindleASIN="B00ELJZZ24", HardboundASIN="--",         PaperbackASIN="1451639228", ImgSrc="http:images.amazon.com/images/P/B00ELJZZ24.01.MZZZZZZZ"},
    new BookClass{Year=1946, YearDisplay="1946", Category="Best Novella", Title="Animal Farm",                                                Author="George Orwell",                KindleASIN="B003ZX868W", HardboundASIN="0151010269", PaperbackASIN="184046254X", ImgSrc="http:images.amazon.com/images/P/B003ZX868W.01.MZZZZZZZ"}
};

...表明这不是数据的问题,除非以下因某些原因确实存在问题:

Title =“他们相当正确(也称为永远的机器)”

更新2

不知怎的,这最终出现在我的getHugos.cshtml文件的顶部 - 打败了我 - :

/P/

......所以第一行是:

/P/@{

......这就是它失败的原因。

对于我来说它甚至可以编译是奇怪的和/或不可思议的 - 事实上,如果不这样做会很好,并指出我的那条线!

但是感谢你对无与伦比的alt标签的提示,JayC - 它并没有导致html失败,但我确定它搞砸了我的。

2 个答案:

答案 0 :(得分:1)

在“终极地球”结束后,你错过了一个逗号。

答案 1 :(得分:1)

好的,现在很清楚了。我不知道你在哪里得到这个htmlBuilder位,但我很清楚,如果不是你的问题,这是一个问题。我们来看看Hugos的html生成器:

$.getJSON('GetHugos', function (data) {
    $.each(data, function (i, dataPoint) {
        if (IsClassYear(dataPoint.Category)) {
            htmlBuilder += '<div class=\"yearBanner\">' + dataPoint.Category + '</div>';
        } else {
            htmlBuilder += '<section class=\"wrapper\" ><a id=\"mainImage\" class=\"floatLeft\" href=\"' +
                Urlify(dataPoint.KindleASIN) + '\"' +
                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\ alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\ ></img></a>' +
                '<div id=\"prizeCategory\" class=\"categorySmallerFont\">' +
                dataPoint.Category +
                '</div><br/><cite id=\"prizeTitle\" >' +
                dataPoint.Title +
                '</cite><br/><div id=\"prizeArtist\" class=\"author\">' +
                dataPoint.Author +
                '</div><br/>';
            if (dataPoint.KindleASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.KindleASIN) + '\"' +
                    ' target=\"_blank\">Kindle</a></button>';
            }
            if (dataPoint.PaperbackASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.PaperbackASIN) + '\"' +
                    ' target=\"_blank\">Paperback</a></button>';
            }
            if (dataPoint.HardboundASIN.trim().length > 2) {
                htmlBuilder += '<button><a href=\"' + Urlify(dataPoint.HardboundASIN) + '\"' +
                    ' target=\"_blank\">Hardcover</a></button>';
            }
            htmlBuilder += '</section>';
        }//else
    }); //each

即使我们假设所有字段都是正确的html编码(通常,你不会假设),这不会产生有效的HTML。

                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\ alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\ ></img></a>' +  

似乎缺少图像属性srcalt的引号结束标记。此外,img不能包含任何子节点,因此只需将其设为单个标记<img />,但严格来说,这不会导致任何问题。

                ' target=\"_blank\"><img height=\"160\" width=\"107\" src=\"' +
                dataPoint.ImgSrc + '\" alt=\"' + dataPoint.Title + ' by ' + dataPoint.Author + ' book cover\" /></a>' +  

一般来说,我通过使用某种客户端模板解决方案来避免这种情况,就像你可能会发现使用underscore.js或者像胡子,把手,knockout.js或Telerik的kendo网格更像花哨的东西。