开发者

Legal JSON giving error in IE: Expected identifier, string or number

I have a strange IE error, and all i can find on google is that the error should come when you have a trailing comma in a dictionary or an array. But since my JSON is perfectly leagal according to JSONLint, i am getting really frustrated.. The JSON is listed here. My question is. Did anyone experience this error where it wasn't ","-related ? Any help or guidance would be greatly apreciated..

By the way the error shows up in any JSON decoder i have tried, but the one i want to use is 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"
    }
}

UPDATE

Ok guys.. i've kind of solved my problem.. The way i was getting the json was from Django, i assumed i could simple take the simplejson.dump of my dictionary and then use it in a template like this:

var opf = {{ var }}

This worked perfectly fine in webkit and mozilla browsers, however IE choked on it with the mysterious error on that exact line. So.. What i tried was

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

and

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

which generated the exact same error but now the error happened somewhere in mootools. I tried everything.. :/ so what i ended up doing, was to make a Request.JSON instead so that i dont pass the variable to the template. Now IE suddently play nicely :/ i feel it is a bit of a hack, but what the heck. Thanks for the quick replies guys..开发者_高级运维


Updated answer:

I think it must be an error elsewhere on the page. Dimitar pointed out below that you tagged your question mootools and so are probably using JSON.decode, but that works fine:

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

Live example

Alternately: If what you've quoted appears literally in your JavaScript code (not enclosed in a string, not being loaded from the server via XHR or something), then what you've quoted is a JavaScript object literal, not JSON, and you don't parse it at all. Simplified example:

JavaScript object literal:

var obj = {
    foo: "bar"
};

Quotes around the property names are optional in an object literal (not so in JSON, where they're required), so this is fine too:

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

JSON:

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

Original answer: (having missed the mootools tag)

It works fine with json2.js, even back on IE6:

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

Live example

Sounds like a problem with how it's being parsed, or perhaps a problem elsewhere in the code.

If you're using raw eval, be sure you wrap the JSON in parentheses:

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

Live example

...but I wouldn't recommend using eval to parse JSON (not even json2, which uses eval under the covers) unless you're sure of the source (e.g., your own server). I'd probably use one of the non-eval parsers from Crockford's github page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜