IE中出现错误的合法JSON:预期的标识符,字符串或数字

时间:2011-03-11 10:52:04

标签: javascript json internet-explorer mootools

我有一个奇怪的IE错误,我在谷歌上找到的只是当你在字典或数组中有一个尾随逗号时会出现错误。但是根据JSONLint,我的JSON完全是leagal,我真的很沮丧..这里列出了JSON。我的问题是。有没有人遇到这个错误,它不是“,” - 相关?任何帮助或指导都会受到很大的影响..

顺便说一句,错误出现在我试过的任何JSON解码器中,但我要使用的是mootools JSON.decode

{
    "spine": [
        "title.xml",
        "about.xml",
        "main0.xml",
        "main1.xml",
        "main2.xml",
        "main3.xml",
        "main4.xml",
        "main5.xml",
        "main6.xml",
        "main7.xml",
        "main8.xml",
        "main9.xml",
        "main10.xml",
        "main11.xml",
        "similar.xml",
        "feedbooks.xml"
    ],
    "chapters": {
        "main9.xml": "TEST",
        "similar.xml": "TEST",
        "title.xml": "TEST",
        "main10.xml": "TEST",
        "feedbooks.xml": "TEST",
        "about.xml": "TEST",
        "main0.xml": "TEST",
        "main5.xml": "TEST",
        "main4.xml": "TEST",
        "main8.xml": "TEST",
        "main6.xml": "TEST",
        "main11.xml": "TEST",
        "main3.xml": "TEST",
        "main1.xml": "TEST",
        "main7.xml": "TEST",
        "main2.xml": "TEST"
    },
    "manifest": {
        "ncx": {
            "href": "fb.ncx",
            "media-type": "application/x-dtbncx+xml"
        },
        "titlepage": {
            "href": "title.xml",
            "media-type": "application/xhtml+xml"
        },
        "logo-feedbooks": {
            "href": "images/logo-feedbooks.png",
            "media-type": "image/png"
        },
        "logo-feedbooks-tiny": {
            "href": "images/logo-feedbooks-tiny.png",
            "media-type": "image/png"
        },
        "about-css": {
            "href": "css/about.css",
            "media-type": "text/css"
        },
        "feedbooks": {
            "href": "feedbooks.xml",
            "media-type": "application/xhtml+xml"
        },
        "main8": {
            "href": "main8.xml",
            "media-type": "application/xhtml+xml"
        },
        "main9": {
            "href": "main9.xml",
            "media-type": "application/xhtml+xml"
        },
        "main0": {
            "href": "main0.xml",
            "media-type": "application/xhtml+xml"
        },
        "main1": {
            "href": "main1.xml",
            "media-type": "application/xhtml+xml"
        },
        "main2": {
            "href": "main2.xml",
            "media-type": "application/xhtml+xml"
        },
        "main3": {
            "href": "main3.xml",
            "media-type": "application/xhtml+xml"
        },
        "main4": {
            "href": "main4.xml",
            "media-type": "application/xhtml+xml"
        },
        "main5": {
            "href": "main5.xml",
            "media-type": "application/xhtml+xml"
        },
        "main6": {
            "href": "main6.xml",
            "media-type": "application/xhtml+xml"
        },
        "main7": {
            "href": "main7.xml",
            "media-type": "application/xhtml+xml"
        },
        "main10": {
            "href": "main10.xml",
            "media-type": "application/xhtml+xml"
        },
        "main11": {
            "href": "main11.xml",
            "media-type": "application/xhtml+xml"
        },
        "newspaper-css": {
            "href": "css/newspaper.css",
            "media-type": "text/css"
        },
        "page-css": {
            "href": "css/page.css",
            "media-type": "text/css"
        },
        "about": {
            "href": "about.xml",
            "media-type": "application/xhtml+xml"
        },
        "main-css": {
            "href": "css/main.css",
            "media-type": "text/css"
        },
        "titlepage-css": {
            "href": "css/title.css",
            "media-type": "text/css"
        },
        "feedbooks-css": {
            "href": "css/feedbooks.css",
            "media-type": "text/css"
        },
        "similar": {
            "href": "similar.xml",
            "media-type": "application/xhtml+xml"
        }
    },
    "spinereal": [
        "title.xml",
        "about.xml",
        "main0.xml",
        "main1.xml",
        "main2.xml",
        "main3.xml",
        "main4.xml",
        "main5.xml",
        "main6.xml",
        "main7.xml",
        "main8.xml",
        "main9.xml",
        "main10.xml",
        "main11.xml",
        "similar.xml",
        "feedbooks.xml"
    ],
    "contents": [
        {
            "src": "title.xml",
            "id": "level1-titlepage",
            "title": "Title"
        },
        {
            "src": "about.xml",
            "id": "level1-about",
            "title": "About"
        },
        {
            "src": "main0.xml",
            "id": "chap578",
            "title": "Chapter 1"
        },
        {
            "src": "main1.xml",
            "id": "chap580",
            "title": "Chapter 2"
        },
        {
            "src": "main2.xml",
            "id": "chap582",
            "title": "Chapter 3"
        },
        {
            "src": "main3.xml",
            "id": "chap584",
            "title": "Chapter 4"
        },
        {
            "src": "main4.xml",
            "id": "chap586",
            "title": "Chapter 5"
        },
        {
            "src": "main5.xml",
            "id": "chap588",
            "title": "Chapter 6"
        },
        {
            "src": "main6.xml",
            "id": "chap590",
            "title": "Chapter 7"
        },
        {
            "src": "main7.xml",
            "id": "chap592",
            "title": "Chapter 8"
        },
        {
            "src": "main8.xml",
            "id": "chap594",
            "title": "Chapter 9"
        },
        {
            "src": "main9.xml",
            "id": "chap596",
            "title": "Chapter 10"
        },
        {
            "src": "main10.xml",
            "id": "chap598",
            "title": "Chapter 11"
        },
        {
            "src": "main11.xml",
            "id": "chap600",
            "title": "Chapter 12"
        },
        {
            "src": "similar.xml",
            "id": "level1-similar",
            "title": "Recommendations"
        }
    ],
    "metadata": {
        "publisher": "TEST",
        "rights": "TEST",
        "description": "TEST",
        "language": "TEST",
        "creator": "TEST",
        "title": "TEST",
        "source": "TEST",
        "coverage": "",
        "date": "TEST",
        "identifier": "TEST",
        "subject": "TEST"
    }
}

更新

好的伙计..我有点解决了我的问题..我得到json的方式是来自Django,我以为我可以简单地采用我的字典的simplejson.dump然后在这样的模板中使用它:

var opf = {{ var }}

这在webkit和mozilla浏览器中运行得非常好,但IE在这条线路上出现了神秘的错误。所以..我尝试的是

var opf = JSON.decode({{var}});

var opf = JSON.decode("{{var}}");

产生完全相同的错误,但现在错误发生在mootools的某个地方。我尝试了所有的东西..:/所以我最终做的,是做一个Request.JSON,以便我不将变量传递给模板。现在IE突然发挥得很好:/我觉得这有点像黑客,但是到底是什么。感谢快速回复家伙..

1 个答案:

答案 0 :(得分:3)

更新回答

我认为页面上的其他一定是错误的。 Dimitar在下面指出你标记了你的问题mootools,所以可能正在使用JSON.decode,但这样可行:

var obj, json;
json = '...your JSON...';
obj = JSON.decode(json);

Live example

另外:如果您引用的内容在JavaScript代码中出现字面(未包含在字符串中,未通过XHR从服务器加载等),那么你所引用的是一个JavaScript对象文字,而不是JSON,你根本就不解析它。简化示例:

JavaScript object literal:

var obj = {
    foo: "bar"
};

属性名称周围的引号在对象文字中是可选的(在JSON中不是这样,需要它们),所以这也很好:

var obj = {
    "foo": "bar"
};

JSON:

var json = '{' +
           '   "foo": "bar"' +
           '}';
var obj = JSON.decode(json);

原始回答(错过了mootools代码)

它适用于json2.js,甚至回到IE6:

var obj, json;
json = '...your JSON...';
obj = JSON.parse(json); // where `json` is a string containing your JSON

Live example

听起来像是如何解析它的问题,或者代码中的其他问题。

如果您使用原始eval,请确保将JSON包装在括号中:

var obj, json;
json = '...your JSON...';
obj = eval('(' + json + ')');

Live example

...但我不建议使用eval来解析JSON(甚至不使用json2,它使用eval),除非你确定来源(例如,你自己的)服务器)。我可能会使用Crockford's github page中的一个非eval解析器。